Misc

Crazy Extension Method

Here is an example of a crazy extension method that alters the semantics of method calling.

First the extension method:

public static class MyExtensions
{
    public static bool IsNullOrEmpty(this string target)
    {
        return string.IsNullOrEmpty(target);
    }
}

Instead of calling the static method IsNullOrEmpty() on string, we are turning it around to allow it to be called on a string type like an instance method. However, as you can probably tell, it may be called when the reference to the string is null. Normally this would result in an exception to say that you are attempting to call a method on a null value. However, this is an extension method and it actually works with nulls! This is probably not the best idea in the world, to be diplomatic about it.

Here is some calling code:

string a = null;
Console.WriteLine(a.IsNullOrEmpty());

Normally, an exception will be thrown if IsNullOrEmpty() is a real method. However, it isn’t in this case and the application happily writes “True” to the console.

 

7 thoughts on “Crazy Extension Method

  1. If you take a look at the IL that’s generated then it makes perfect sence. In effect the compiler will rewrite:a.IsNullOrEmpty();With:MyExtensions.IsNullOrEmpty(a);So therefore it’s perfectly valid, you’ve got to remember that’s what the compiler is doing, simply saving you a few keystrokes, it’s not trying to do anything cleverer than that.Actually this could be very useful for writing methods that “execute” on null objects, thus eliminating some try/catch handling perhaps?

  2. @Ed, In terms of whot the compiler does: Absolutely. I wrote about how extension methods are implemented way back (http://blog.colinmackay.net/archive/2007/06/18/20.aspx)In terms of the semantics: The craziness lies in the fact that semantics of C# in general say you cannot call a method on a reference that is null, and if you try then you will get a NullReferenceException. Extension methods break the semantics in this case, and therefore runs contrary to best practice.If you simply attempt to call a method on null itself you get a compiler error “Operator ‘.’ cannot be applied to operand of type ‘<null>'”.

  3. @ColinYou’re thinking too deep man! Actually I’d be curious if anyone else picks on this fact and to see what “interesting” concepts can come out of it…

  4. >>In terms of the semantics: The craziness lies in the fact that semantics of C# in>>general say you cannot call a method on a reference that is null>>Extension methods break the semantics in this case>>and therefore runs contrary to best practice.Not quite.Extension methods breaks the rules of 2.0 code, sure.But since it is a new feature from c#3 / .net 3.5 , then you cannot say it is breaking any rules..the rules have just changed.. there are new rules now..So in your case, you are applying knowledge of an old environment onto a new environment.(Also, it have been perfectly legal to call instance methods on null references at IL level from .NET 1.. not saying that you should, just that you could)

  5. @Roger Alsing: I never said anything about breaking rules. Syntactically it is valid. Semantically it is not. You basically have two things which look the same but operate differently.Regardless, it is not best practice to mix them up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s