Tuesday, March 19, 2013

Extension methods in c#


Today, we are going to take a look on extension methods. This feature is introduced in C#.NET 3.0. Extension methods defined in the "System.Linq" namespace. For LINQ developers, the use of extension methods begins when you start working with LINQ itself.

What are Extension Methods?


Extension methods are a special kind of static method that allows you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
                                                                       Or
Extension methods are a special kind of static method that allows you to add new methods to existing types without creating derived types.

All the extension methods have that little blue down arrow as part of their icon, and the tool tip will have the word "(extension)" in front of the method name.



Number of reasons behind the popularity of extension methods. flexibility is one of them. However there are two vital reasons to use extension methods-

 You can add new functionality in extension methods -
  • Without recompiling the existing type.
  • Without touching the original assembly.
Extension Methods
  • Defined in a static class
  • Defined as static
  • Use “this” keyword before its argument to specify the class to be extended.
  • Can also be called from statically through the defining static class.
  • Extension methods can’t access the private methods in the extended type. 

Extension Methods with Example

public static class WordCount
{
     public static int WordsCount(this String mStr) 
     {
        return mStr.Split(new char[]{ ' ', '.', '?' },
        StringSplitOptions.RemoveEmptyEntries).Length;
     } 
}


Complete Code:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace extensionmethods
{
   public static class WordCount
   {
     public static int WordsCount(this String mStr)
     {
        return mStr.Split(new char[] { ' ', '.', '?' },
        StringSplitOptions.RemoveEmptyEntries).Length;
      }
   }


 class Program
 {
     static void Main(string[] args)
     {
           string strName =
"Hello Friends";
           Console.WriteLine(strName .WordsCount());
           Console.ReadLine();
     }
  }



Binding Extension Methods at Compile Time

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.


At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if a type has a method named WordsCount (string name) and you have an extension method with the same signature, the compiler will always bind to the instance method.
When the compiler encounters a method invocation, it first looks for a match in the type's instance methods. If no match is found, it will search for any extension methods that are defined for the type, and bind to the first extension method that it finds.