C#, Linq

Extension methods

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

e.g which add extension method in string data type.

Defination of extension method

public static class test
{
    public static bool EqualWithIgnoreCase(this string str, string str2)
    {
        return (str.ToLower().Equals(str2.ToLower()));
    }
}

Uses:

string name = "Amol";
if (name.EqualWithIgnoreCase("amol"))
{
    //do something
}

(for more info visit: http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx)

One thought on “Extension methods

Leave a comment