.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Call Stack, Call Site and Stack unwinding Tutorial

Posted By:Dhananjay Kumar       Posted Date: August 13, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

In this tutorial, I will explain three basic terms of C#: Call Stack, Call Site, Stack Unwinding.
 

Objective 


In this article, I will explain three basic terms of C#
  1. Call Stack 
  2. Call Site 
  3. Stack Unwinding 
You can also follow my blog

Let us say, you got the below code 


Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication7
{
   class Program
   {
       static void Main(string[] args)
       {
           Method1(); 
           Console.Read(); 
       }
       static void Method1()
       {
           Method2();
           Console.WriteLine("Method1");
       }
       static void Method2()
       {
           Method3();
           Console.WriteLine("Method2");
       }
       static void Method3()
       {
           Method4();
           Console.WriteLine("Method3");
       }
       static void Method4()
       {
           Method5();
           Console.WriteLine("Method4");
       }
       static void Method5()
       {
           Console.WriteLine("Method5");
       }
   }
}

I have just created five dummy methods and calling them in nested manner.  The methods, I have created are Method1 to Method5. If you run the above program, you will get output as below 


1.gif 


If you examine the output, Methods are being called in reverse order. 


Program is putting the methods in stack as below, 


2.gif 


What is Call Stack? 


Here, we are calling a method inside a method and so on and this is called CALL STACK.


3.gif

Call Stack is the process of calling method inside a method.  


Mathematically, we can say 


4.gif

What is Stack Unwinding? 


Once the method call is completed that method is removed from the stack and this process is known as Stack Unwinding.


5.gif

What is Call Site?


In above program, we are calling Method3 from Method2, so we can say Method2 is call site of Method3


6.gif

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend