Basic of C#: Call Stack, Call Site and Stack unwinding

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

clip_image002

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

Program is putting the methods in stack as below,

clip_image003

What is Call Stack?

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

clip_image004

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

Mathematically, we can say

clip_image006

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.

clip_image008

What is Call Site?

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

clip_image010

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Posted in Miscellaneous
3 comments on “Basic of C#: Call Stack, Call Site and Stack unwinding
  1. Lohith says:

    Hey

    Nice article man. Simple and crisp.

    regards
    Lohith

  2. Great work, keep it up!

    For me, reviewing the call stack of recursive functions is a challenge. :S

  3. Free Calls says:

    I am a little bit weak in computer language. is there any other way you can define these terms in simple english?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • after 2 sixes of Sami .. I say LOL -> Virat KOHLI ... happy to see Kohli the ILL ATTITUDE MAN LOOSING .. 1 hour ago
  • It does not matter from where are you coming ? only does matter where are you going ? #djsays 2 hours ago
  • With people who participated in WCF day .. Great hosting by @CsharpCorner amd great leadership of @mcbkruse http://t.co/HkHjCdveJC 3 hours ago
  • RT @petekrueger: I've been using @Icenium. I would definitely recommend checking it out if you're tackling cross-platform mobile: http://t.… 3 hours ago
  • मुझे ग़म है की मैंने जिंदगी में कुछ नहीं पाया , ये ग़म दिल से निकल जाये अगर तुम मिलने आ जाओ 4 hours ago
Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,123 other followers

%d bloggers like this: