<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET Framework</title>
        <link>http://geekswithblogs.net/ftom/category/8599.aspx</link>
        <description>.NET Framework</description>
        <language>en-US</language>
        <copyright>ftom</copyright>
        <managingEditor>aft3000@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>C# Cheating Polymorphism: Interfaces are not virtual by nature, or are they?</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/11/24/c-cheating-polymorphism-interfaces-are-not-virtual-by-nature-or.aspx</link>
            <description>&lt;p&gt;After being in TFS-Land for a couple of months and celebrating the birth of my second daughter, I finally had time to go back to do some C# coding. Hurray! &lt;/p&gt;
&lt;p&gt;Then, I ran into an interesting issue:&lt;br /&gt;
When you derive from a class which has an interface implemented you cannot just override the implement methods again. Even if you use the new keyword breaks when you cast the instance to the interface type, you might not achieve the desired result. &lt;/p&gt;
&lt;p&gt;Let's look at a quick example and deal with solutions afterwards:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ITom
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay();
}

&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; BaseClass : ITom
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()
    {
        &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Base Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}

&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass : BaseClass
{
    &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//I have to use the new keyword&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()
    {
        &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"New Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}


DerivedClass d = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass();
Console.WriteLine(d.IdeaOfTheDay());
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: New Idea&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;

ITom t = d &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;as&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ITom;
Console.WriteLine(t.IdeaOfTheDay());
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: Base Idea  -&amp;gt; oops not what I hoped for&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Okay this shouldn't have come as a surprise to me. When I thought about it for a second, I realized it is standard polymorphism with contracting to a specific non virtual method. Using interfaces made me forget this for a second.&lt;/p&gt;
&lt;p&gt;If we didn't have interaces and we could not change the BaseClass to virtual or make the BaseClass abstract,  we would be done here. Even using the new keyword does not work if we cast to an interface, because the actual contract is with the BaseClass.&lt;br /&gt;
Pretty bad when you deal with customer dlls where you cannot modify the source code.&lt;/p&gt;
&lt;p style="FONT-WEIGHT: bold"&gt;Cheating Polymorphism with Interfaces in an emergency&lt;/p&gt;
&lt;p&gt;But with interfaces there is one more thing you can do if you can not change the BaseClass: &lt;br /&gt;
Just re-implement the Interface!&lt;br /&gt;
&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; BaseClass : ITom&lt;br /&gt;
{&lt;br /&gt;
   &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()&lt;br /&gt;
    {&lt;br /&gt;
       &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Base Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass : BaseClass, &lt;strong&gt;ITom&lt;/strong&gt; &lt;br /&gt;
{&lt;br /&gt;
   &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//We still need the keyword&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;br /&gt;
   &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()&lt;br /&gt;
    {&lt;br /&gt;
       &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"New Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
DerivedClass d =&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass();&lt;br /&gt;
Console.WriteLine(d.IdeaOfTheDay());&lt;br /&gt;
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: New Idea&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;br /&gt;
&lt;br /&gt;
ITom t = d&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;as&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ITom;&lt;br /&gt;
Console.WriteLine(t.IdeaOfTheDay());&lt;br /&gt;
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: New Idea NOT: Base Idea anymore&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Kind of messy but this can be a lifesaver!&lt;/p&gt;
&lt;p style="FONT-WEIGHT: bold"&gt;Using Polymorphism correctly with Interfaces&lt;/p&gt;
&lt;p&gt;But if you can modify the BaseClass you definitely want to use virtual methods. &lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: #bbbbff"&gt;Even though Interfaces are not virtual by nature, you can still declare the method virtual in your class implementation and make them virtual!&lt;br /&gt;
This gives you the best out of both worlds: Polymorphism from Inheritance and Contracting via Interfaces. &lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; BaseClass : ITom
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="FONT-WEIGHT: bold; COLOR: #0000ff"&gt;virtual&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()
    {
        &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Base Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}

&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass : BaseClass{
    &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//normal overriding&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="FONT-WEIGHT: bold; COLOR: #0000ff"&gt;override&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IdeaOfTheDay()
    {
        &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"New Idea"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}


DerivedClass d = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DerivedClass();
Console.WriteLine(d.IdeaOfTheDay());
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: New Idea&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;

ITom t = d &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;as&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ITom;
Console.WriteLine(t.IdeaOfTheDay());
&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//Writes: New Idea NOT: Base Idea anymore&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Think about it: An interace is just a contract! Nothing more, nothing less!&lt;/p&gt;
&lt;p&gt;Tom&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127342"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127342" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/127342.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/11/24/c-cheating-polymorphism-interfaces-are-not-virtual-by-nature-or.aspx</guid>
            <pubDate>Mon, 24 Nov 2008 16:04:23 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/127342.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/11/24/c-cheating-polymorphism-interfaces-are-not-virtual-by-nature-or.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/127342.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/127342.aspx</trackback:ping>
        </item>
        <item>
            <title>C# and the using Statement in 3 seconds and a bug in Reflector</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/09/15/c-and-the-using-statement-in-3-seconds-and-a.aspx</link>
            <description>&lt;strong&gt;Using() Statement in 3 seconds and a bug in Reflector &lt;/strong&gt;
&lt;p style="BACKGROUND-COLOR: #bbbbff"&gt;The boring, known accross the board definition from the MSDN site: &lt;br /&gt;
Defines a scope, outside of which an object or objects will be disposed&lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: #9999ff"&gt;The more interesting definition from Tom: &lt;br /&gt;
The using() Statement generates a try{} finally{ //virtual call of the dispose method } block with null check!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Let's take a deeper look.&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;Let's write two test methods first:&lt;br /&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #008000"&gt;//Testmethod for Using   &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; UsingTest()
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; (SqlConnection con = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SqlConnection())
    {
        con.ConnectionString = &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}

&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//TestMethod for try finally&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; TryFinallytest()
{
    SqlConnection con = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SqlConnection();
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;try&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    {
        con.ConnectionString = &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;finally&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    {
        con.Dispose();
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The TryFinallyTest() method has no null check inside the finally block. This means in case con is null the function would throw a Null Reference Exception. The UsingTest() method does not. &lt;/p&gt;
&lt;p&gt;When you compile both methods and look at them disambeled in Reflector they both look exatly the same:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; UsingTest()
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; (SqlConnection con = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SqlConnection())
    {
        con.ConnectionString = &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}

&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; TryFinallytest()
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; (SqlConnection con = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SqlConnection())
    {
        con.ConnectionString = &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;But looking at the IL (see below) reveals a very important difference:&lt;br /&gt;
-  Frist both get translated into a try{} finally {} block. (see IL code below). This is what we expected&lt;br /&gt;
-  &lt;strong&gt;The using(){} statement adds a null check before calling the dispose method inside the finally.&lt;/strong&gt; &lt;br /&gt;
-  The compiler did not add a null check for the TryFinallyTest method (as intented) &lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: #bbbbff"&gt;&lt;strong&gt;The Problem:&lt;/strong&gt;&lt;br /&gt;
The &lt;strong&gt;TryFinallyTest method gets disasembeld from IL into C# (or VB) by using a using() statement &lt;/strong&gt;. &lt;br /&gt;
This is not correct because a using() statement has a null check before calling the dispose() method.&lt;br /&gt;
&lt;strong&gt;This means the disasembled C# code does not relfect the IL correctly&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Here is the IL code for the UsingTest() method:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #000000"&gt;.method &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; hidebysig &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;strong&gt;UsingTest()&lt;/strong&gt; cil managed
{
    .maxstack &lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;2&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    .locals init (
        [&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;] &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.SqlClient.SqlConnection con,
        [&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;1&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;] &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;bool&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; CS$&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;4&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;$&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;0000&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
    L_0000: nop 
    L_0001: newobj instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.SqlClient.SqlConnection::.ctor()
    L_0006: stloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    L_0007: nop 
    L_0008: ldloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    L_0009: ldstr &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    L_000e: callvirt instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.Common.DbConnection::set_ConnectionString(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
    L_0013: nop 
    L_0014: nop 
    L_0015: leave.s L_0027
    L_0017: ldloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;     
    L_0018: ldnull  
    L_0019: ceq 
    L_001b: stloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.1&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    L_001c: ldloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.1&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    &lt;strong&gt;L_001d: brtrue.s L_0026    //Nullcheck&lt;/strong&gt;
    L_001f: ldloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    &lt;strong&gt;L_0020: callvirt instance &lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;strong&gt; [mscorlib]System.IDisposable::Dispose()  //dispose call&lt;/strong&gt;
    L_0025: nop 
    L_0026: endfinally 
    L_0027: nop 
    L_0028: ret 
    &lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;try&lt;/span&gt;&lt;span class="style1" style="COLOR: #000000"&gt; L_0007 to L_0017 &lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;finally&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;strong&gt; handler L_0017 to L_0027&lt;/strong&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: L_001d: brtrue.s perfoms the null check. In case con is null it jumps directly to the endfinally. This means in case con is null there is no Null Reference Exception&lt;/p&gt;
&lt;p&gt;Here is the IL code for the TryFinallyTest method:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #000000"&gt;.method &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; hidebysig &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;strong&gt;TryFinallytest()&lt;/strong&gt; cil managed
{
    .maxstack &lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;2&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    .locals init (
        [&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;] &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.SqlClient.SqlConnection con)
    L_0000: nop 
    L_0001: newobj instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.SqlClient.SqlConnection::.ctor()
    L_0006: stloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    L_0007: nop 
    L_0008: ldloc&lt;/span&gt;&lt;span style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; 
    L_0009: ldstr &lt;/span&gt;&lt;span style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    L_000e: callvirt instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [System.Data]System.Data.Common.DbConnection::set_ConnectionString(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
    L_0013: nop 
    L_0014: nop 
    L_0015: leave.s L_0021
    L_0017: nop 
    &lt;strong&gt;L_0018: ldloc&lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #ff0000"&gt;.0&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;   &lt;/span&gt;&lt;span class="style2"&gt;//it loads con and calls directly dispose in the next line. No null check&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    &lt;strong&gt;L_0019: callvirt instance &lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;strong&gt; [System]System.ComponentModel.Component::Dispose()&lt;/strong&gt;
    L_001e: nop 
    L_001f: nop 
    L_0020: endfinally 
    L_0021: nop 
    L_0022: ret 
    &lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;try&lt;/span&gt;&lt;span class="style1" style="COLOR: #000000"&gt; L_0007 to L_0017 &lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;finally&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;strong&gt; handler L_0017 to L_0021&lt;/strong&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Inside the the finally block the IL loads con on the Excution Stack and calls dispose() directly without a null check. In case con is null, an Null Reference Exception is thrown. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So, what to use? Using() or try{} finally{}?&lt;/strong&gt;&lt;br /&gt;
Well, usually I would recommend using Using(), so you don't have to remember the null check. &lt;br /&gt;
But if you have multiple using inside eachother I would rather use one try{} finally{}.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;If you don't know (or if you are too lazy to check)  if your object implements IDisposable you can always use the following pattern to be on the save side:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ObjectAsDispose(SqlConnection con)
{
    &lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;using&lt;/span&gt;&lt;span class="style1" style="COLOR: #000000"&gt; (con &lt;/span&gt;&lt;span class="style1" style="COLOR: #0000ff"&gt;as&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&lt;strong&gt; IDisposable)
    {
        con.ConnectionString = &lt;/strong&gt;&lt;/span&gt;&lt;span class="style1" style="COLOR: #800000"&gt;"Test Connection"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;&lt;strong&gt;
    }&lt;/strong&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;I hope you enjoyed this one again. &lt;/p&gt;
&lt;p&gt;Tom&lt;/p&gt;
&lt;p&gt;P.S.: Thanks Ian for reminding me to add the safty pattern for using!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125189"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125189" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/125189.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/09/15/c-and-the-using-statement-in-3-seconds-and-a.aspx</guid>
            <pubDate>Mon, 15 Sep 2008 21:03:06 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/125189.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/09/15/c-and-the-using-statement-in-3-seconds-and-a.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/125189.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/125189.aspx</trackback:ping>
        </item>
        <item>
            <title>C# Events and Delegates in 3 seconds </title>
            <link>http://geekswithblogs.net/ftom/archive/2008/09/12/c-events-and-delegates-in-3-seconds.aspx</link>
            <description>&lt;p&gt;C#, Events and Delegates as an Elevator Pitch - Or in 3 seconds &lt;/p&gt;
&lt;p&gt;Here are the elevator pitches you should know about events and delegates:&lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: #9999ff"&gt;&lt;br /&gt;
&lt;strong&gt;   A Delegate is a (or collection of) strongly typed FunctionPointer(s) &lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;   An Event is a wrapped Delegate&lt;/strong&gt;  &lt;br /&gt;
 &lt;/p&gt;
&lt;p&gt;But to be honest this is not big news and not the reason for this blog. I thought this would make a nice starter... Like when you go for a nice dinner.  Today I had the chance to look under the hood and I was astonished to see what really happens. This made me think... hmm sounds like an interesting post for Friday afternoon. (Thank you Mark!)&lt;/p&gt;
&lt;p&gt;So let's take a second to verify the second statement. An Event is a wrapped Delegate:&lt;/p&gt;
&lt;p&gt;First we define an Event and subscribe to it:&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #008000"&gt;//Test class with the event&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; TomsEventClass
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;event&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; EventHandler ItsTomTime;
}

&lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//test program&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Program
{
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Main(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;[] args)
    {
        TomsEventClass tom = &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; TomsEventClass();
        tom.ItsTomTime += tom_ItsTomTime;
    }
    &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; tom_ItsTomTime(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; sender, EventArgs e)
    {
       &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//let's see what we can do it&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;But what was actually generated by the compiler? Let's use Reflector to look at the IL&lt;/p&gt;
The Event gets translated into one private delegate:
&lt;pre&gt;&lt;span style="COLOR: #000000"&gt;.field &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;private&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [mscorlib]System.EventHandler ItsTomTime
&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
... and into two public methods to add and remove Subscribers:
&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;.event&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [mscorlib]System.EventHandler ItsTomTime
{
   .addon instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ConsoleApplication3.TomsEventClass::add_ItsTomTime(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [mscorlib]System.EventHandler)
   .removeon instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ConsoleApplication3.TomsEventClass::remove_ItsTomTime(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [mscorlib]System.EventHandler)
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And when we subscribe we see we actually call the generated add_ItsTomTime method: &lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: #000000"&gt; callvirt instance &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ConsoleApplication3.TomsEventClass::add_ItsTomTime(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; [mscorlib]System.EventHandler)
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;That's it! &lt;br /&gt;
&lt;br /&gt;
I have to admit, even knowing that events where restriced delegates I never took the time to really look under the hood. &lt;br /&gt;
This won't make me a better programer but it is fun to know (at least for a Geek on geekswithblogs :-)&lt;/p&gt;
&lt;p&gt;Tom&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125141"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125141" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/125141.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/09/12/c-events-and-delegates-in-3-seconds.aspx</guid>
            <pubDate>Fri, 12 Sep 2008 21:35:55 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/125141.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/09/12/c-events-and-delegates-in-3-seconds.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/125141.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/125141.aspx</trackback:ping>
        </item>
        <item>
            <title>C# and the difference between out and ref</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/09/10/c-and-the-difference-between-out-and-ref.aspx</link>
            <description>&lt;strong&gt;C#: The difference between out and ref&lt;/strong&gt;.
&lt;p&gt;Today we had an interesting discussion about &lt;strong&gt;out&lt;/strong&gt; and &lt;strong&gt;ref&lt;/strong&gt; in C#. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Here is a very quick elevator pitch summary:&lt;/p&gt;
&lt;p&gt;The MSDN documentation (&lt;a href="http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx&amp;quot;&amp;gt;http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx"&gt;http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx"&amp;gt;http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx&lt;/a&gt;) is very good on that and it says: &lt;br /&gt;
"The &lt;strong&gt;out&lt;/strong&gt; keyword causes arguments to be passed by reference. This is similar to the &lt;strong&gt;ref&lt;/strong&gt; keyword, except that &lt;strong&gt;ref&lt;/strong&gt; requires that the variable be initialized before being passed." &lt;/p&gt;
&lt;p&gt;This means inside your method implementation which takes an &lt;strong&gt;out&lt;/strong&gt; parameter you have to set a value before you can use it. At least you have to assign a value before you can return. &lt;br /&gt;
This also means you can not use the &lt;strong&gt;out&lt;/strong&gt; paramter to pass a value to the method. (With &lt;strong&gt;ref&lt;/strong&gt; you can) &lt;br /&gt;
This also means you can not have any logically includes comparison (e.g. if(param&amp;gt;0 ){ ...}) inside your method before you assign a value to your incoming parameter (I know this sounds weird).&lt;/p&gt;
&lt;p&gt;Also nice to know: &lt;br /&gt;
"The &lt;strong&gt;ref&lt;/strong&gt; and &lt;strong&gt;out&lt;/strong&gt; keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a &lt;strong&gt;ref&lt;/strong&gt; argument and the other takes an &lt;strong&gt;out&lt;/strong&gt; argument."&lt;/p&gt;
&lt;p&gt;To see the difference in IL, I used Reflector to see how the IL looks like for &lt;strong&gt;out/ref &lt;/strong&gt; on Value Types (in my case int32):&lt;/p&gt;
&lt;p style="FONT-FAMILY: Courier"&gt;&lt;strong&gt;out&lt;/strong&gt;:&lt;br /&gt;
[&lt;font color="#1000a0"&gt;out&lt;/font&gt;] int32&amp;amp; result&lt;/p&gt;
&lt;p style="FONT-FAMILY: Courier"&gt;&lt;strong&gt;ref&lt;/strong&gt;:&lt;br /&gt;
int32&amp;amp; result&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Remarks&lt;/span&gt;:&lt;br /&gt;
- Most of the Framework functions (e.g. TryParse) use &lt;strong&gt;out&lt;/strong&gt; and not &lt;strong&gt;ref &lt;/strong&gt; (which was surprising to me). But inside the internal implementation the Framework mainly uses &lt;strong&gt;ref&lt;/strong&gt;.&lt;br /&gt;
- Properties can not be passed via &lt;strong&gt;out&lt;/strong&gt; or &lt;strong&gt;ref&lt;/strong&gt;. (Under the hood they are function calls)&lt;br /&gt;
- It is very handy if you have several Value Types as return values and you don't want to create a class or struct.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125063"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=125063" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/125063.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/09/10/c-and-the-difference-between-out-and-ref.aspx</guid>
            <pubDate>Wed, 10 Sep 2008 16:29:12 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/125063.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/09/10/c-and-the-difference-between-out-and-ref.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/125063.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/125063.aspx</trackback:ping>
        </item>
        <item>
            <title>C# (or .NET): Primitive Types, Value Types, Reference Types and System.ValueType. </title>
            <link>http://geekswithblogs.net/ftom/archive/2008/09/04/c-or-.net-primitive-types-value-types-reference-types-and.aspx</link>
            <description>C# (or .NET): Primitive Types, Value Types, Reference Types and System.ValueType.
&lt;p&gt;I think this is one of the most important topics in .NET, but it is interesting to see what kind of misunderstandings are out there. Even with programmers who have used .NET for years.&lt;/p&gt;
&lt;p&gt;DISCLAIMER 1:&lt;br /&gt;
The goal of this blog entry is not to give another in depth, all-encompasing overview - but rather a summary of the conversations I've had with people and a summary of some confusing MSDN documentation. Also I won't address the issue of what to use when and the wonderful side effects (which is indeed very important to understand but I'll leave it for another time). There are really good articles out there that do a way better job than I could anyway. Click &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc301569.aspx"&gt;here&lt;/a&gt; for one of them. &lt;/p&gt;
&lt;p&gt;But let's get back to business here... &lt;/p&gt;
&lt;p&gt;In the following I will refer to one MSDN article as MSDN. You can find it  &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc301569.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;DEFINITIONS (based on MSDN):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Primitive Types:&lt;/strong&gt;&lt;br /&gt;
MSDN: Any data types directly supported by the compiler are called primitive types &lt;br /&gt;
TOM: e.g. int-&amp;gt;System.Int32, long -&amp;gt; System.Int64&lt;br /&gt;
Complete List of Types: &lt;a href="http://msdn.microsoft.com/en-us/magazine/bb984984.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/bb984984.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value Types:&lt;/strong&gt;&lt;br /&gt;
MSDN (on top of the page): A type declared using struct is a Value Type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reference Types:&lt;/strong&gt;&lt;br /&gt;
Types declared using class are reference types.&lt;/p&gt;
&lt;p&gt;Hence:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RESULT 1:&lt;/strong&gt; A primitive Type is not the same then a Value Type! (I don't know how often I heart people saying that they are. Including me in the past :-)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;QUESTION 1:&lt;/strong&gt; What about Enum? Aren't they Value Types too?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;QUESTION 2:&lt;/strong&gt; How do we test for Value Types?&lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: rgb(187,187,255)"&gt;&lt;strong&gt;ANSWER 1:&lt;/strong&gt; The MSDN article gives a well known answer a little bit later: &lt;br /&gt;
&lt;strong&gt;Value types are implicitly derived from &lt;span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,255); FONT-FAMILY: Courier"&gt; System.ValueType !&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;QUESTION 3:&lt;/strong&gt; Are structs derived from System.ValueType too? I thought they are "the" Value Type by Definition? &lt;/p&gt;
&lt;p&gt;Before we answer question 3, let's answer question 2 and use it as a springboard to answer this.&lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: rgb(187,187,255)"&gt;&lt;strong&gt;ANSWER 2:&lt;/strong&gt;&lt;br /&gt;
Test for Value Type (according to ANSWER 1). Click &lt;a href="http://geekswithblogs.net/ftom/archive/2008/08/27/why-it-is-time-to-retire-the-old-cast-operator.aspx"&gt;here&lt;/a&gt; for details. &lt;/p&gt;
&lt;strong&gt;
&lt;pre style="BACKGROUND-COLOR: rgb(187,187,255)"&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; (x &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;is&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; ValueType) { //yep x is a Value Type }&lt;/span&gt;&lt;/pre&gt;
&lt;/strong&gt;
&lt;p&gt;Now let's use answer 2 to answer question 3:&lt;br /&gt;
(I hope you are totally confused about all the questions and answers by now :-)&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;struct&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; TomStruct&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; First { &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;get&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;set&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;; }&lt;br /&gt;    &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; Last { &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;get&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;set&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;; }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; ToString()&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; First + &lt;/span&gt;&lt;span style="COLOR: rgb(128,0,0)"&gt;", "&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; +Last;&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TomStruct p1 = &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; TomStruct() { First = &lt;/span&gt;&lt;span style="COLOR: rgb(128,0,0)"&gt;"Tom"&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;, Last = &lt;/span&gt;&lt;span style="COLOR: rgb(128,0,0)"&gt;"Fischer"&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; };&lt;br /&gt;&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; (p1 &lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;is&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt; System.ValueType)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="COLOR: rgb(128,0,0)"&gt;"TomStruct derives from System.ValueType"&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,0)"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;And as we suspeceted the output verifies that struct (or Enum) derives from ValueType!&lt;/p&gt;
&lt;p&gt;So let's try a new Tom Definition for ValueTypes:&lt;/p&gt;
&lt;p style="BACKGROUND-COLOR: rgb(187,187,255)"&gt;&lt;strong&gt;DEFENTIION for Value Types:&lt;/strong&gt;&lt;br /&gt;
Every type, deriving from &lt;span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,255); FONT-FAMILY: Courier"&gt; System.ValueType &lt;/span&gt;is a Value Types. This includes types defined via &lt;span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,255); FONT-FAMILY: Courier"&gt;struct&lt;/span&gt; or &lt;span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,255); FONT-FAMILY: Courier"&gt;enum &lt;/span&gt;. &lt;/p&gt;
&lt;p&gt;(I bet now I forgot a type here:-) But at least this Defintion does not exclude it :-) You gotta love logic!&lt;/p&gt;
&lt;p&gt;I hope you had some fun with me here. Sometimes the most basic stuff is the easiest to get confused about :-)&lt;br /&gt;
Looking forward to reading lots of comments,&lt;/p&gt;
&lt;p&gt;Tom&lt;/p&gt;
&lt;pre&gt; &lt;/pre&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124939"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124939" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/124939.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/09/04/c-or-.net-primitive-types-value-types-reference-types-and.aspx</guid>
            <pubDate>Thu, 04 Sep 2008 19:22:29 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/124939.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/09/04/c-or-.net-primitive-types-value-types-reference-types-and.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/124939.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/124939.aspx</trackback:ping>
        </item>
        <item>
            <title>ToString() and the underestimated IFormattable  ( IFormatPorvider, ICustomFormatter) - Why to use it!</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/08/29/tostring-and-the-underestimated-iformattable---iformatporvider-icustomformatter.aspx</link>
            <description>&lt;p&gt;                         ToString() and the underestimated IFormattable     &lt;/p&gt;
&lt;p&gt;         Everyone knows you always should override the ToString() function on you custom          objects. But most programmers (including me until yesterday) leave it there.&lt;/p&gt;
&lt;p&gt;I ran into a problem these days where I wanted a specific xml representation and a          tap separated representation of the same custom object. The "experienced"          programmer would just write their own format function. But it felt more old          fashion and not          quite OO like.&lt;/p&gt;
&lt;p&gt;         And then I read the Tip 5 in the book Effective C# programming - and it change          my life again (there is lot's of life changing going on :-)&lt;/p&gt;
&lt;p&gt;         I totally agree with Scott Meyers about IFormattable but I have a slightly          different approach for implementing, which makes it even easier to read (at least          from my perspective)&lt;/p&gt;
&lt;p&gt;         &lt;strong&gt;So what is IFormattable?         &lt;span style="color: rgb(0, 0, 0);"&gt; IFormatProvider? ICustomFormatter?  Why are          there so many?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;         The quick answer is: Don't worry about IFormatProvider or ICustomFormatter for          most of your stuff.&lt;/p&gt;
&lt;p&gt;             &lt;strong&gt;Implementing IFormatProvider via the pattern described below is          all you will need...  usually!&lt;/strong&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;         When you implement IFormattable you have to implement one function (hurray that's              it):         &lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ToString(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; format, IFormatProvider formatProvider)&lt;/span&gt;&lt;/pre&gt;
You can use         &lt;br /&gt;
- either only the first string parameter  (then you have access to all your          private/protected members)&lt;br /&gt;
- or pass you own implementation of how you want to have it formatted. (Then you          only have public members available)
&lt;p&gt;     I personally think for most of the cases the first option is easier and good enough.      But it was fun yesterday to play around with the implementation of IFormattable.      First it was not straight forward but after getting to know it (especially      realizing the pattern the .NET team chose to use)- it is pretty      cool (Ended up being a Factory Pattern)&lt;/p&gt;
&lt;p&gt;     Here is the code for a simple class which implements IFormattable. &lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType : IFormattable&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;virtual&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Name { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; First { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ToString(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt; format&lt;/strong&gt;, IFormatProvider formatProvider)&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (formatProvider != &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;br /&gt;        {&lt;br /&gt;            ICustomFormatter fmt = formatProvider.GetFormat(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.GetType()) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ICustomFormatter;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (fmt != &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fmt.Format(format, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, formatProvider); }&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;switch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt; (format)&lt;br /&gt;        {&lt;br /&gt;            &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;case&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);" class="style1"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);" class="style1"&gt;"a"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;strong&gt;                 &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);" class="style1"&gt; First + &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);" class="style1"&gt;", "&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt; + Name;&lt;br /&gt;            &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;case&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);" class="style1"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);" class="style1"&gt;"first"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;strong&gt;                 &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt; First;&lt;br /&gt;            &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;case&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);" class="style1"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);" class="style1"&gt;"G"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;strong&gt;             &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;default&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;strong&gt;                 &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);" class="style1"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt; Name;&lt;br /&gt;        }&lt;/strong&gt;     } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;     Note: First we check if the consumer wants to use a custom formater or if we      just want to use our internal formating option. Simple but very effective. &lt;/p&gt;
&lt;p&gt;     The case statement is the actual place where all the "magic" happens.     &lt;br /&gt;
Note: Make sure that there is always an implementation for G. In my case I use the      default in the switch, so I could have skipped it. &lt;/p&gt;
&lt;p&gt;         This already solved my problem with the two ToString() Implementations I needed: plain and xml&lt;/p&gt;
&lt;p&gt;     &lt;strong&gt;But let's look at the more fancy way and see how a custom Formatter works.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;     The implementation of  IFormatPorvider is like the name says a provider aka      "Factory Pattern". You choose the actual Formatter (worker) class, depending on      the calling Type. In our case the implementation of IFormatProvider checks for      our Type (TomType) and returns an instance (a "Singleton Pattern" could also be      instead) of the actual implementation: TomsCustomFormatter:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; CustomFormatProvider : IFormatProvider&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetFormat(Type formatType)&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (formatType == &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(TomType))&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomsCustomFormater();&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomsCustomFormater : ICustomFormatter&lt;br /&gt;{&lt;br /&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Format(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; format, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg, IFormatProvider formatProvider)&lt;br /&gt;    {&lt;br /&gt;        TomType t = arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (t == &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg.ToString(); }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"TomsCustomFormater: "&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; + t.First + &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;", "&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; + t.Name;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;         If we run a little demo now:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Main(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] args)&lt;br /&gt;{&lt;br /&gt;    TomType t = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType() { Name = &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"Fischer"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, First = &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"Tom"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; };&lt;br /&gt;&lt;br /&gt;    Console.WriteLine(t.ToString(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"a"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;));&lt;br /&gt;    Console.WriteLine(t.ToString(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"first"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;));&lt;br /&gt;    Console.WriteLine(t.ToString(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; CustomFormatProvider()));&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;             We will get the expected result:&lt;/p&gt;
&lt;p&gt;             &lt;span style="font-family: Courier;"&gt;Tom, Fischer&lt;/span&gt;&lt;br class="style2" /&gt;
&lt;span style="font-family: Courier;"&gt;Tom&lt;/span&gt;&lt;br class="style2" /&gt;
&lt;span style="font-family: Courier;"&gt;TomsCustomFormater: Tom, Fischer&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;         That's it. I hope you enjoyed this as much as I did it when I stumbled over it&lt;/p&gt;
&lt;p&gt;         Tom&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124792"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124792" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/124792.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/08/29/tostring-and-the-underestimated-iformattable---iformatporvider-icustomformatter.aspx</guid>
            <pubDate>Fri, 29 Aug 2008 15:30:54 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/124792.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/08/29/tostring-and-the-underestimated-iformattable---iformatporvider-icustomformatter.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/124792.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/124792.aspx</trackback:ping>
        </item>
        <item>
            <title>Why it is time to retire the old cast operator in C#: MyType t= (MyType)o</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/08/27/why-it-is-time-to-retire-the-old-cast-operator.aspx</link>
            <description>Why it is time to retire the old cast operator in C#:  MyType t= (MyType)o
&lt;p&gt; These days I am reading the book "Effective C#: 50 Specific Ways to improve C#" by Scott Meyers. After using C# for many years, you get into the mindset: "I know everything already". But this book really inspires me to rethink the basics I am using every day and make a step forward in sharpening my C# skills.&lt;/p&gt;
&lt;p&gt;The goal of these blog series is not to copy the book but write down my thoughts in a compressed way on certain topics. So when I will wonder in the future "Why am I doing this" I can just come back and re-read it and also maybe for you guys who don't have the time to give you a quick overview of the little things we use everyday without knowing.&lt;/p&gt;
&lt;p&gt;So enough talk time for the topic: &lt;br /&gt;
&lt;strong&gt;Why is it time to retire the old cast operator in C#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Don't get me wrong there are reasons (maybe 0.05%) but it is time to explore the better casting options we have in C#:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 255); font-family: Courier;"&gt;is&lt;/span&gt;,&lt;span style="font-weight: bold; color: rgb(0, 0, 255); font-family: Courier;"&gt; as&lt;/span&gt; and &lt;span style="font-weight: bold; color: rgb(0, 0, 255); font-family: Courier;"&gt;typeof &lt;/span&gt;are the new heroes!&lt;/p&gt;
&lt;p&gt;So what is the big difference in a nutshell:&lt;/p&gt;
&lt;p&gt;is compares if an object is of specific type and returns true or false.  Every is operator could be replaced with &lt;br /&gt;
as will attempt to cast it into the specific type and returns either the type or null&lt;br /&gt;
typeof checks if the actual type of the object (A comparison of a derived type does not match the parent type) &lt;/p&gt;
&lt;p&gt;But let's have a look at some code:&lt;/p&gt;
&lt;p&gt;First same basic class definition:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DeriveTomType : TomType&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Age { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;virtual&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Name { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now the actual test code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; o = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DeriveTomType();&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(o &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;is&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"is: o is TomType"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//no try catch required. If cast fails, t will be null. &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//No exception thrown&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;br /&gt;TomType t = o &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (t != &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"as: o is TomType"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//the same in one line:&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ((t = o &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TomType) != &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"as Replacement: o is TomType"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;( o.GetType()!= &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(TomType)) &lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"GetType: o is not TomType"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
And the result is
&lt;p&gt; is: o is TomType &lt;br /&gt;
as: o is TomType &lt;br /&gt;
as: o is TomType &lt;br /&gt;
&lt;strong&gt;getType: o is not TomType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The interested reader might think: "Wow this is cool. But why should I not use the cast operator anymore again?"&lt;/p&gt;
&lt;p&gt;And the quick answer is: &lt;br /&gt;
- No try catch required =&amp;gt; Easier on the CLR (this is the main reason)&lt;br /&gt;
- Easier to read code  (very important to me)&lt;/p&gt;
&lt;p&gt;I know there is more to it, but for this blog entry I think this is good enough. &lt;br /&gt;
Otherwise I would contradict my intention: Keep it short AND GOOD ENOUGH!&lt;/p&gt;
&lt;p&gt;Tom&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124726"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124726" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/124726.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/08/27/why-it-is-time-to-retire-the-old-cast-operator.aspx</guid>
            <pubDate>Wed, 27 Aug 2008 16:27:39 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/124726.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/08/27/why-it-is-time-to-retire-the-old-cast-operator.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/124726.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/124726.aspx</trackback:ping>
        </item>
        <item>
            <title>The big side affect of  const versa readonly</title>
            <link>http://geekswithblogs.net/ftom/archive/2008/08/26/the-big-site-affect-of--const-versa-readonly.aspx</link>
            <description>&lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Arial"&gt;The big side affect of  &lt;span style="font-family: Courier New; font-weight: bold;"&gt;const&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;vs. &lt;span style="font-family: Courier New; font-weight: bold;"&gt;readonly&lt;/span&gt;.... &lt;/font&gt;Back into C#!&lt;/font&gt;&lt;font face="Arial"&gt;&lt;br /&gt;
After spending a couple of days at DevLink (which I really enjoyed) I am back in C# code.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I just stumbled over a very interesting fact, which I knew that there is an important difference but was not aware of some interesting side affect. It was an eye-opener, so I thought I have to share this one with you. If you already know this - good for you :-)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The big side affect of &lt;/font&gt;&lt;font face="Arial"&gt;&lt;font face="Arial"&gt;&lt;span style="font-family: Courier New; font-weight: bold;"&gt;const&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;vs. &lt;span style="font-family: Courier New; font-weight: bold;"&gt;readonly:&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;First some theory (I guess most of you already know it - but just for the record): &lt;br /&gt;
- const expression get replaced at compile time with the actual value.&lt;br /&gt;
- If you reference a DLL with a const the value of the const gets written into your dll at compile time&lt;br /&gt;
- const can only be used for numbers strings and apparently structs&lt;br /&gt;
- readonly gets assigned at runtime. &lt;br /&gt;
- readonly can be set only in the constructor or direct reference&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So lets look at a small example:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;In an separate assembly (e.g. Infrastructure) you define&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;!--l version="1.0--&gt;&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MyConst&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Start = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; End = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;15&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt; &lt;font face="Arial"&gt;In a different project you use:&lt;br /&gt;
 &lt;/font&gt; &lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i = MyConst.Start; i &amp;lt; MyConst.End; i++)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"i={0}"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, i);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;font face="Arial"&gt;The expected output is:&lt;br /&gt;
i=0&lt;br /&gt;
..&lt;br /&gt;
i=14&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So now we change the value in our  seperate Assembly to:&lt;/font&gt;&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MyConst&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Start = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;const&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; End = &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;115&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Because we did not change our actual project we only deploy our  seperate assembly and run it again.... &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;strong&gt;And nothing happens&lt;/strong&gt;... &lt;strong&gt;The loops does not even start &lt;/strong&gt;...Ooops&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;Why? We already gave the answer in the theory section:&lt;br /&gt;
- &lt;strong&gt;During compile time the compiler replaces End with 15 and not a reference to the actual object&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;So simple and mind-blowing!&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;I hope you also enjoyed this one with me&lt;br /&gt;
Tom&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;/font&gt; P.S.: This one is for you Ian!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124703"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124703" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ftom/aggbug/124703.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ftom</dc:creator>
            <guid>http://geekswithblogs.net/ftom/archive/2008/08/26/the-big-site-affect-of--const-versa-readonly.aspx</guid>
            <pubDate>Tue, 26 Aug 2008 18:55:55 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ftom/comments/124703.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ftom/archive/2008/08/26/the-big-site-affect-of--const-versa-readonly.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ftom/comments/commentRss/124703.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ftom/services/trackbacks/124703.aspx</trackback:ping>
        </item>
    </channel>
</rss>