Hi Guys
NP102 Please correct it
http://www.aspfree.com/c/a/C-Sharp/C-sharp-Methods-part-1/4/
I got the following program from the above website. Output expected from the program is:
Void
Void Method1()
Void Method2()
Void Method3()
Void Method4()
But program is giving different output please correct it.
Thank you
using System;
using System.Diagnostics;
namespace Methods
{
class Class1
{
static void
{
Method1();
Console.ReadLine();
}
static void Method1()
{
Method2();
}
static void Method2()
{
Method3();
}
static void Method3()
{
Method4();
}
static void Method4()
{
StackTrace st = new StackTrace();
for (int i = st.FrameCount - 1; i >= 0; i--)
{
StackFrame sf = new StackFrame();
sf = st.GetFrame(i);
Console.WriteLine(sf.GetMethod());
}
}
}
}
Posted Jun 20, 2008, 8:59 AM
What I mean is same Software but different version (Microsoft Visual C# 2008 Express Edition & Microsoft Visual Studio.NET C# 2003) is giving bit different output. I wish to know the reason for that. Please explain.
Jan MontanoPosted Jun 19, 2008, 11:56 PM
I'm not sure how to explain this well. Just think of stack frames as a history of events (methods). It lists events/methods called sequentially.
Before reaching method4, we have to pass several events. The first one will have to be the main method, then from main, Method1 is called. but from method1, method2 is called, then method3 and method4.
Intuitively, the stack frame would look similar to
Main
Method1
Method2
Method3
Method4
Posted Jun 19, 2008, 10:35 AM
I found the problem. It is a change in the environment caused different output. I have Microsoft Visual Studio.NET C# 2003 as well and I tried the above program in that environment.
When I execute the program in the above environment the program output is similar to author’s version that means out put is:
VoidMain (System.String[])
Void Method1()
Void Method2()
Void Method3()
Void Method4()
Please explain the reason.
Posted Jun 19, 2008, 7:20 AM
http://www.aspfree.com/c/a/C-Sharp/C-sharp-Methods-part-1/4/
I use Microsoft Visual C# 2008 Express Edition which is a free down load. I use this to run C# programs.
When I use “Start Without Debugging” (Ctrl+F5) in the drop down menu to execute the above program the program output is:
VoidMain (System.String[])
Void Method4()
When I use “Step Into” that means key F11 to execute the same program the out put is similar to your one. Please explain the reason for variation.
Not only that in the above website author has shown the program out put which is
VoidMain (System.String[])
Void Method1()
Void Method2()
Void Method3()
Void Method4()
That is why I am expecting the above out put.
Posted Jun 19, 2008, 2:46 AM
Jan MontanoPosted Jun 18, 2008, 10:56 PM
Long time no see. It gives me this output. Wouldn't the expected output be really something like this? As long as you have the Main followed by method1, 2... 4, I think it's OK.
Void ThreadStart()
Void Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, Sy
stem.Object)
Void ThreadStart_Context(System.Object)
Void RunUsersAssembly()
Int32 ExecuteAssembly(System.String, System.Security.Policy.Evidence, System.Str
ing[])
Int32 _nExecuteAssembly(System.Reflection.Assembly, System.String[])
Void Main(System.String[])
Void Method1()
Void Method2()
Void Method3()
Void Method4()