C# Methods, part 1 - Methods and StackFrame
(Page 5 of 6 )
When the CLR calls a method, there are steps for executing the internal method's code. The first step is to allocate a memory space called a Stack Frame to hold the method's local variables (including the argument values that has been passed to the method). The Stack Frame is a block of memory that the CLR allocates just before calling the method, and it will be dislocated just after the execution of the method ends, so you can say that it's a temporary block of memory. The CLR calculates the space of the Stack Frame based on the number and the Data Type of the method's parameters and local variables, so you don't have to know anything about it, but we discuss it here to improve your knowledge.
Inside a method you will find a call to another method, and so on; it's basic Object oriented Programming Principles that methods call other methods. The CLR keeps track of what methods have been called inside your method, and so on, by building a stack trace, which is an ordered collection of the stack frames that have been created so far for the current method.
Actually, the Debugging Features of Microsoft Visual Studio.NET use the StackFrame and StackTrace classes extensively to provide you with information for what methods have been called, along with the values of the arguments. These classes lives in the namespace System.Diagnostics, and you can use it to navigate through the method calls that happened in the application. Let's look at an example:
using System;
using System.Diagnostics;
namespace Methods
{
class Class1
{
static void Main(string[] args)
{
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());
}
}
}
}
The result is:

As you can see, we received the order of the method calls that have been called in an ordered list. The Main method had a call to Method1, which in turn called Method2, which in turn called Method3, which in turn called Method4 -- and in Method4 we create a StackTrace object and then loop on the StackTrace object to print each StackFrame that has been created for each method. Actually, if you take a look at the for loop, you will find that I use a reserve order to print the method calls exactly as we did through the code, but some programmers write the code in the following way, which prints the Method4 first (because it's the one that has created the code):
static void Method4()
{
StackTrace st = new StackTrace();
for(int i = 0; i < st.FrameCount; i++)
{
StackFrame sf = new StackFrame();
sf = st.GetFrame(i);
Console.WriteLine(sf.GetMethod());
}
}
The result will be

By using the namespace System.Diagnostics, you can create debugging tools, and you may create a runtime debugger for your application, which is very useful in scientific applications.
Next: Methods Overloading >>
More C# Articles
More By Michael Youssef