"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far."

Extension methods in .Net framework

Recently one of my friend asked me about extension method. Though it was introduced in .Net framework 3.0, lots of people are not using this or not aware of the power of extension methods. So I decided to write a blog post about this to give an introduction to the extension methods.

In a nutshell Extension methods make the .Net framework extensible. You can add methods to existing classes without altering or accessing the original class code. This applies to in-built types that shipped with .Net framework and the types you create. Using this feature you can extend types/classes that you never have access to. You can add a method to a class written by somebody else. Extension method adds more power to .Net framework.

Prior to extension methods, developers were using the inheritance feature to extend a class. You need to create a new class based on the existing class and can add methods to it. This is great, but this approach has two disadvantages.

  • In order to add a single method, you need to create a new class. So just to add a simple method you are forced to extend a class and create a new class/Type, which is inconvenience for developers.
  • Secondly there are classes with definition Sealed (Not Inheritable in Visual Basic) as you can not extend such classes. There is lots of classes in .Net framework with sealed definition which makes almost impossible to add more properties or methods to those classes. E.g. System.String, System.Int32 etc.

Starting with .Net framework 3.0 there is happy news for developers. A new feature added to the framework called “Extension Methods”. Extension methods allow developers to define methods to existing classes, great feature! Right? Now developers can add new methods to existing classes even if the Class is sealed(Not Inheritable).

I am going to demonstrate how you can make use of Extension method in C#. For the purpose of this demonstration, I am going to add a method to the type “int”. The method name is “IsEven” which will return a Boolean value depending whether the number is even or not.

By default, when you define an int variable there are certain methods available. Visual studio IntelliSense will display all the available methods. The following is the screenshot of the default ones available with int.

clip_image001

Now I am going to add IsEven to the list of methods available for Int. To create an extension method in C#, you need to create a public static class and define a public static method in it. The first argument for the method should be the type you are extending (int in our case). Also you need to mark the first argument with this keyword to denote that it is an extension method.

For e.g. consider the following method definition

public static bool IsEven(this int num){……}

This defines an extension method for the type int that returns a Boolean value. If you place this method inside a public static class, the framework will understand this as an extension method and will be available for the type you extended.

See the definition of the Extension class I have created.

clip_image002

Once I create this class, the Visual Studio IntelliSense will identify the method in the available list as follows.

clip_image003

See the sample code that illustrates the use of IsEven method

clip_image004

You can add methods to built-in classes and for user defined classes. By using this, you can add more methods to existing types that can be used by your team of developers. Visual Studio IntelliSense is incredible in this as it finds and lists all available methods. As a developer you should make use of extension methods to simplify common tasks.

4 Comments

Comments have been disabled for this content.