C#
  Home arrow C# arrow Page 5 - C# Methods, part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Methods, part 1
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 31
    2005-04-26

    Table of Contents:
  • C# Methods, part 1
  • Method Signature
  • Instance Methods
  • Static Methods
  • Methods and StackFrame
  • Methods Overloading

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT