Extension methods are interesting

By NoMoreHacks

Not my idea, but it amused me. Thanks Dave.

namespace ExtensionMethodsAreEvil
{
    class ExtensionMethodsAreEvil
    {
        public static void Main()
        {
            string myNullString = null;
            Console.WriteLine("Is the string empty? " + myNullString.IsNullOrEmpty());
        }
    }

    public static class ExtendString
    {
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }
    }
}

4 Responses to “Extension methods are interesting”

  1. Julien Says:

    I’m missing the point, sorry Dave :-) !

    I would also add that it is a very border line usage in my opinion, because it’s changing an important behaviour of the framework, that is throwing exceptions when calling a method on a null object.
    When you start changing basic behaviours such as these, god knows where you’ll end up :-)

  2. test0191 Says:

    Sorry I should have put “interesting”; i.e., horrible! This is completely wrong, wrong, wrong. But it works as comedy, I think.

  3. Julien Says:

    Fair enough in that case :D

  4. Sebastien Lambla Says:

    No, what’s evil is the VB extension methods, because you can pass the this pointer as a ref parameter. Now *thats* evil. :)

Leave a Reply