Welcome to Blog.Radovici.Com Sign in | Join | Help

Eldar's thoughts

Disclaimer: Do not use while operating a motor vehicle or heavy equipment.

<April 2008>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Post Categories

Navigation

External Links

Syndication

$100 bet regarding extension methods

An extension method is C# syntactic sugar that adds intellisense functionality to an encapsulated class.

Let's look at an example to see how this works:

public static class AlcoholicString
{
    public static bool IsDrunk(this string str)
    {
        return str.Contains("beer"); //what if i'm null?
    }
}

This returns true for both AlcoholicString.IsDrunk("eldar wants a beer") and "eldar wants a beer".IsDrunk(). The latter is an example of an extension method.

Now the $100 question is does this work for:

string strTest = null;
strTest.IsDrunk(); //will this compile? will this run? what does it return?

Here's the MSDN article.

Published Tuesday, April 08, 2008 8:07 PM by eldar

Comments

No Comments

Anonymous comments are disabled
Powered by Community Server, by Telligent Systems