Extension methods in C#


Extension methods were a new feature added in C# 3.0 It is one of the most useful concepts added in the .net framework and are widely used in application development due to its ease of creation and use. For those who are familiar with the concept of linq, will be aware of the extension methods very well as they are used when we write linq queries using lamda expressions. For ex. we have different extension methods available in it like Where(), Any(), Count() etc. .

We can create our own extension methods in C#, and bind them to any type like integer, string, or any other class defined by us. Creating an extension method is as simple as using these methods. No extra skills are required for this.

So, our first step will be to create a static class that will be encapsulating the extension method. Next we will be creating an extension method, which will be linked to the type integer. Our method will also be a static here. See the code below :

1

This method will work on the input parameter of integer and return true or false, based on the value of the input value.

The most important point to note here is the input parameter in the function. Notice the keyword thisthis keyword specifies that the extension method is meant to be used for the integer types only.

Next step is to use this method. Include the reference to the class CustomExtensionMethods, and declare any variable of type integer. Then to use this method, simply use the variable name and then this method will be available for use. See the code below :

2

So whatever value you assign to the input parameter will be evaluated and result will be calculated based on that. Similarly, you can create these methods for any type like string, object or even your own defined classes. So basically it helps you to extend the functionality of any existing type, like we did for the integer type.

Few points that should be kept in mind while creating these methods :

1. If they are created on the type object, then these methods will be available to every type in your framework i.e., it becomes globally available in string, boolean etc..

2. If you have any method in the base class, which is having the same signature as your extension method, then that method will override your extension method i.e. your method will not be called as the extension methods have low priority as compared to the instance methods that you define (Refer to : http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx)  .

Happy coding…!!!

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in C#. Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s