Call Stack, Call Site and Stack unwinding in C#

No.of Views894
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  17 Aug 2010 11:08:58
Tag : CSharp , General
In this article, I will explain three basic terms of C#, such as Call Stack ,Call Site and stack Unwinding
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I will explain three basic terms of C#

1.    Call Stack
2.    Call Site
3.    Stack Unwinding

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 

Image Loading

If you examine the output, Methods are being called in reverse order. Program is putting the methods in stack as below, 

Image Loading

What is Call Stack?

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

Image Loading

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

Mathematically, we can say 

Image Loading

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. 

Image Loading

What is Call Site?

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

Image Loading

That's all. happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    I can see, there is lots of people is discussing about it in MSDN forums, few people wanted to get rid of the Task from the async methods and really want to deal with normal return types, while others just wanted to get rid of the async postfix
    Published Date : 25/Nov/2010
    Are you somewhat confused between Serialization and Marshaling? This writing would break this confusion up, it would give you a basic understanding of the process of Serialization and the process of Marshaling, and how you can get the most out of each.
    Published Date : 10/May/2010
    First, this writing concentrates of and compares between three programming languages, C#, C++/CLI, and ISO/ANSI C++. It discusses 9 rules that every developer should keep in mind while working with constructors, destructors, and finalizers and class hierarchies:
    Published Date : 05/May/2010
    This article explains how to filter rows in a DataSet/DataTable. The example provided will help you get information faster.
    Published Date : 23/May/2010
    In this article, I will discuss about Checked and unchecked keyword and conversions in C#.
    Published Date : 16/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top