Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C# 4.0

How does it work in C#? - Part 2

Rate me:
Please Sign up or sign in to vote.
4.81/5 (42 votes)
4 Feb 2013CPOL7 min read 102.5K   1.8K   150   34
How does throw, rethrow and where, select clause of Enumerable class work in C# programming language.

Contents

  • Introduction
  • How does it work?
    • throw an exceptionObject
    • Where and Select of Enumerable
  • Limitations
  • History
  • Reference

Source code and demo

Introduction

In the first part of How does it work in C#? article I discussed about the var, auto-implemented properties and += and -= of events in C#. In this article, I will be discussing about throwing an exception and how does it work and what is the internal mechanism for these, what is internal working mechanism of where and select clause of Enumerable class in C#.

How does it work?

In the following discussion we will see the internal working mechanism about the throw statement and where, select of Enumerable class.

throw an exceptionObject

Exception is one of the common scenarios for any application. As a result, proper exception management is one of the important tasks in application development. In here, I would like to discuss about throw statement in C#, how does throw anExceptionObject; or just throw works. I wrote a small program which will help me to explain the details. The task of this program is simple, it will raise an exception and throw it,

C#
namespace TestHarnessPart2
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Person person = new Person();
            }
            catch (Exception exceptionObject)
            {
                throw;
            }
        }
    }
    public class Person
    {
        public Person()
        {
            throw new Exception("Exception from Person.");
        }
    }
}

The above code catch the exception and just throw it with out passing any explicit exception object.I created another version of this above code for example,

C#
namespace TestHarnessPart2
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Person person = new Person();
            }
            catch (Exception exceptionObject)
            {
                throw exceptionObject;
            }
        }
    }
    public class Person
    {
        public Person()
        {
            throw new Exception("Exception from Person.");
        }
    }
}

The above code will create a Person object and if any exception occurred during this creation it will catch that exception and from the catch block it throws the catched exception which is throw exceptionObject;. Now if we see the stack trace of those above code we will find out there is difference, stack trace for first version of the code is as below,

HowitworksCSharpTwo/StackTraceThrow.PNG

Fig: Stack trace of the first version of the code.

And stack trace of the second version of the code,

HowitworksCSharpTwo/StackTraceThrowException.PNG

Fig: Stack trace of the second version of the code

From the above two images we can see the details of the stack trace is different.Now the question is how does it happens, to get the answer we will get help of ILDasm program, grab the exe of TestHarnessPart2 from the bin folder and drop into the ILDasm program , for the first version of the code we will see something like below,

HowitworksCSharpTwo/ILThrow.PNG

Fig :IL code for using just throw in the catch block.

From the above image we can see exceptionObject has been defined as local variable using .locals init [1] but in the catch block compiler change the throw statement into

C#
catch [mscorlib]System.Exception 
{
   IL_000b:  stloc.1
   IL_000c:  nop
   IL_000d:  rethrow
}  // end handler

rethrow, which means compiler does not change the original stack trace of the exception object it is re- throwing the existing one, where as if we look into the following image for the second version of the above C# code,

HowitworksCSharpTwo/ILThrowExceptionObject.PNG

Fig: IL code for using just throw excetpionObject in the catch block.

We can see exceptionObject has been defined as local variable as well, using the same way for the first version of the code for example, .locals init [1]. But in the catch block it is doing totally different stuffs compare to first version of the above code. In the catch block compiler load location of 1( ldloc.1) which is the exceptionObject

C#
catch [mscorlib]System.Exception
{
  IL_000b:  stloc.1
  IL_000c:  nop
  IL_000d:  ldloc.1
  IL_000e:  throw
}  // end handler

and throw that one. As a result, this exceptionObject will not hold all the stack trace raised earlier except the stack trace from this current state. Now the last bit of the puzzle is, what actually throw and rethrow statement does? From the Partition III CIL.doc retrieved from ECMA C# and Common Language Infrastructure Standards we can see what actually throw and rethrow does,

IL Instruction Description
throw Throw an exception. The throw instruction throws the exception object (type O) on the stack and empties the stack. So in relation to the second version of the code block it will be,
C#
[1] class [mscorlib]System.Exception 
exceptionObject
rethrow Rethrow the current exception. The rethrow instruction is only permitted within the body of a catch handler. It throws the same exception that was caught by this handler. A rethrow does not change the stack trace in the object.

So it is clear that throw exceptionObject override the stack trace where as, just throw statement does not override the stack trace.

Where and Select of Enumerable

Before we start discussing about Where and Select, just have a quick look of the where and select clause’s signature,

HowitworksCSharpTwo/WhereSignature.PNG

Fig: Signature of the Where clause

HowitworksCSharpTwo/SelectSignature.PNG

Fig: Signature of the Select clause

Where and Select is two extension methods of Enumerable class in .Net. Following sections will discuss about the internal of Where and Select, how does it work. Before, we go ahead just bit of heads up about Func in .Net. According to MSDN, we can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. A simple example of Func below,

C#
namespace TestHarnessPart2
{
    using System;
    public class ExampleOfFunc
    {
        public int TestFunc(string dataToCheck, Func<string,> getLength)
        {
            return getLength(dataToCheck);
        }
    }
}

and to test the above code,

C#
private static void TestFuncExample()
{
    ExampleOfFunc exampleOfFunc = new ExampleOfFunc();
    Console.WriteLine(exampleOfFunc.TestFunc("Example of Func", (dataToTest) => dataToTest.Length));
}

So from the above code we can see TestFunc method accepting a Func as parameter and execute it just by doing return getLength(dataToCheck);, from the caller of this method is actually passing an anonymous method block for the Func parameter. Through out the entire discussion about where and select we will find this concept used many places,

Before we go ahead I like to show a bit of code created to explain the where and select statement,

C#
namespace TestHarnessPart2
{
    using System.Collections.Generic;
    using System.Linq;
    class WhereSelect
    {
        List<string> bookList = new List<string>() { "Einstein: His Life and Universe", "Ideas And Opinions", "The World As I See It " };
        public IEnumerable<string> GetBookListWhichLengthIsGreaterThan(int lengthOfTheBookName)
        {
            //return bookList.Where(book => book.Length == lengthOfTheBookName).Select(book => book);
            return bookList.Where(book =>
            {
                var currentBook = book;
                return book.Length > lengthOfTheBookName;
            }).Select(book =>
            {
                var currentBook = book;
                return book;
            });
        }
    }
}

The above code is not doing much rather check the booklist whether it has book which name length is greater than given length (lengthOfTheBookName). Now we will try to find out bit of internal working mechanism of Where and Select clause, where and select clause is defined in the Enumerable class and internal of the where clause is as below,

HowitworksCSharpTwo/WhereInternal.PNG
Fig: Where internal

From the above image we see Where method is calling another internal private class named WhereListIterator<T> which is doing all the work for us which is filter the booklist List based on the condition provided and return output.

C#
private class WhereListIterator<tsource> : Enumerable.Iterator<tsource>
{
      public override bool MoveNext()
      {
        // most of the code has been removed for simplicity
        while (this.enumerator.MoveNext())
        {
          TSource current = this.enumerator.Current;
          if (this.predicate(current))
          {
            base.current = current;
            return true;
          }
        }
      }
}

From the above code we can see this.predicate(current)) line of code is actually executing the Func or anonymous method block in this case, <GetBookListWhichLengthIsGreaterThan>b__0 method. On the other hand, select clause is working like below,

HowitworksCSharpTwo/SelectInternal.PNG

Fig: Select internal

Also, from the above image we can see that there is one selector which is actually <GetBookListWhichLengthIsGreaterThan>b__1 method to select the item from the list. Now need to find out where is this <GetBookListWhichLengthIsGreaterThan>b__1 and

<GetBookListWhichLengthIsGreaterThan>b__0
coming from? Please have a look image below, from the following image we can there is two method named <GetBookListWhichLengthIsGreaterThan>b__1 used for selector and
<GetBookListWhichLengthIsGreaterThan>b__0 used for predicate of the where clause.

HowitworksCSharpTwo/OutputOfTheWhereSelectWithPredicates.PNG

Fig: The output of WhereSelect test

Where and Select clause is using those two methods for doing the operation. Bit of more investigation to find out about it. Now we need the help of .Net Reflector and ILDasm. First grab the TestHarnessPart2.exe from the bin folder and drop into .Net Reflector, then we will find out following interesting stuff, a class <>c__DisplayClass3 with following details,

C#
[CompilerGenerated]
private sealed class <>c__DisplayClass3
{
    // Fields
    public int lengthOfTheBookName;
    // Methods
    public bool <GetBookListWhichLengthIsGreaterThan><getbooklistwhichlengthisgreaterthan>b__0(string book)
    {
        string currentBook = book;
        return (book.Length > this.lengthOfTheBookName);
    }
}

From the class we can see a method named <GetBookListWhichLengthIsGreaterThan>b__0 which is actually containing the filtering condition we defined in the where clause,

C#
{
     var currentBook = book;
     return book.Length > lengthOfTheBookName;
}

And also there is also another method (outside of <>c__DisplayClass3 class) named <GetBookListWhichLengthIsGreaterThan>b__1

C#
[CompilerGenerated]
private static string <GetbookListWhichLengthIsGreaterThan><getbooklistwhichlengthisgreaterthan>b__1(string book)
{
    string CS$<>8__locals4 = book;
    return book;
}

Which is actually containing the statement we defined for the select clause as below,

C#
{
     var currentBook = book;
     return book;
}

Now to prove the above stuff, we will look at the IL code retrieved from ILDasm(also grab the TestHarnessPart2.exe from the bin folder and drop into ILDasm program) for the above C# code,

HowitworksCSharpTwo/ILCodeForWhereSelect.PNG

Fig: IL code of the GetBookListWhichLengthIsGreaterThan method

From the above image we can see where clause is using <GetBookListWhichLengthIsGreaterThan>b__0 method of <>c__DisplayClass3 class and select clause is using <GetBookListWhichLengthIsGreaterThan>b__1 method. So it is clear now how does it work.

Limitation

  • There are no discussions about Query Expression and Lambda Expression.

History

  • Version 1

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
QuestionShould have been 2 articles Pin
Philippe Mori2-Oct-13 3:51
Philippe Mori2-Oct-13 3:51 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-Jul-13 23:43
Humayun Kabir Mamun18-Jul-13 23:43 
GeneralVery helpful Pin
Bandi Ramesh15-Feb-13 22:20
Bandi Ramesh15-Feb-13 22:20 
GeneralRe: Very helpful Pin
Mohammad A Rahman15-Feb-13 22:40
Mohammad A Rahman15-Feb-13 22:40 
QuestionPurpose of the article? Pin
Cindy Meister5-Feb-13 21:11
Cindy Meister5-Feb-13 21:11 
AnswerRe: Purpose of the article? Pin
johannesnestler16-Jul-14 3:52
johannesnestler16-Jul-14 3:52 
GeneralMy vote of 5 Pin
ravithejag4-Feb-13 23:31
ravithejag4-Feb-13 23:31 
GeneralRe: My vote of 5 Pin
Mohammad A Rahman5-Feb-13 12:16
Mohammad A Rahman5-Feb-13 12:16 
QuestionIs the Select clause necessary? Pin
George Swan4-Feb-13 21:46
mveGeorge Swan4-Feb-13 21:46 
QuestionHere's my 5 Pin
skydger30-Nov-12 22:02
skydger30-Nov-12 22:02 
AnswerRe: Here's my 5 Pin
Mohammad A Rahman6-Jan-13 21:47
Mohammad A Rahman6-Jan-13 21:47 
SuggestionConfusion between 'clause', 'statement', 'method'... Pin
Kordalia29-Oct-12 22:55
Kordalia29-Oct-12 22:55 
QuestionMy vote of 1 Pin
MacSpudster13-Jun-12 12:39
professionalMacSpudster13-Jun-12 12:39 
AnswerRe: My vote of 1 Pin
Mohammad A Rahman13-Jun-12 13:35
Mohammad A Rahman13-Jun-12 13:35 
GeneralRe: My vote of 1 Pin
Winsa4-Feb-13 16:37
Winsa4-Feb-13 16:37 
GeneralRe: My vote of 1 Pin
Mohammad A Rahman4-Feb-13 17:27
Mohammad A Rahman4-Feb-13 17:27 
AnswerRe: My vote of 1 Pin
Mohammad A Rahman3-Oct-12 18:26
Mohammad A Rahman3-Oct-12 18:26 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey1-Mar-12 18:02
professionalManoj Kumar Choubey1-Mar-12 18:02 
GeneralRe: My vote of 5 Pin
Mohammad A Rahman1-Mar-12 21:58
Mohammad A Rahman1-Mar-12 21:58 
QuestionGood work Pin
Member 840383415-Nov-11 2:59
Member 840383415-Nov-11 2:59 
AnswerRe: Good work Pin
Mohammad A Rahman15-Nov-11 3:14
Mohammad A Rahman15-Nov-11 3:14 
QuestionMy vote of 5 Pin
Abinash Bishoyi14-Nov-11 4:40
Abinash Bishoyi14-Nov-11 4:40 
AnswerRe: My vote of 5 Pin
Mohammad A Rahman14-Nov-11 5:05
Mohammad A Rahman14-Nov-11 5:05 
Questionwhat does this line of code do? "var currentBook = book;" Pin
coldear13-Oct-11 7:50
coldear13-Oct-11 7:50 
AnswerRe: what does this line of code do? "var currentBook = book;" Pin
Mohammad A Rahman13-Oct-11 11:43
Mohammad A Rahman13-Oct-11 11:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.