Latest Entries »

Extension Methods

Extension methods allows you to add new functionality for existing data types Without modifying the code of the type itself  .This means if you want to add some method to an existing “String” class you can do it quite easily . This allows you to create new methods for a class which you have no access or those classes which are sealed and you can’t inherit. Extension Methods were introduced in C# 3.0.

An extension method is a static method of a static class, where the “this” modifier is applied to the first parameter. The type of the first parameter will be the type that is extended. For example:

String Extension Method

In the Above Example the following is an Extension Method

as you can see the method name itself say what it does it removes numbers from the string and returns only string following would be the output that I would get when I execute the above demo

String Demo output

But there are some rules to it

  • Extension methods cannot be used to override existing methods
  • An extension method with the same name and signature as an instance method will not be called
  • The concept of extension methods cannot be applied to fields, properties or events
  • Use extension methods sparingly….overuse can be a bad thing!

So let us see a demo. Now I would change the name of the “RemoveNumeric" to a method which is an instance method of string that is the “ToString()" as shown below.
Now let us run the Example and check what the output is.

It shows the above output so why I extension method was not called it is because of the following two rules.

  • Extension methods cannot be used to override existing methods
  • An extension method with the same name and signature as an instance method will not be called

You can write extension Methods Not only to specific data types but you can also write Generic Extension Methods as shown below

Generic Extension

In the above Case the extension method is not only accessible to the string class but also the int.

The OutputToConsole is a generic type of Extension Method
Extension Methods can be applied not only to class but also to Interface

.Net also uses a combination of Extension Methods and Generic delegates
as you can see in the example below

 

Here is how we can use Lambda Expression to call a recursive function to calculate the factorial,

Func<int, int> CallMethod = null;
CallMethod = x => x * (x == 1 ? 1 : CallMethod(x - 1));
Console.WriteLine(CallMethod(5));
Console.ReadLine();

This can also be done using the anonymous methods. Anonymous methods were introduced in .net 2.0 and can be used in place of delegate.

delegate int RecursiveMethod(int value);

        static void Main(string[] args)
        {
            RecursiveMethod CallMethod = null;
            CallMethod = delegate(int value)
            {
                return value * (value == 1 ? 1 : CallMethod(value - 1));
            };
            Console.WriteLine(CallMethod(5));
            Console.ReadLine();
        }