Apr 14, 2011

C# gotcha

Consider you have the following code:

SomeClass reference = null;
Console.WriteLine(reference.Echo("echo"));

Your first thought might be “NullReferenceException”. Ha, think again! What if you have an class defining an extension method like this:



public static class SomeClassExt
{
public static string Echo(this SomeClass x, string str)
{
return str;
}
}

You have been warned :)