Extension methods in C#


What are Extension methods ?

Introduced in .NET 3.0, they are one of the most widely used feature today. If you have been using LINQ, then you must have been using the methods like FirstOrDefault, ToList, SingleOrDefault etc.. All these methods are nothing but the extension methods.

So to be more precise, they are the static methods defined in a class, but most importantly, can be used with built in types like int, string etc., without the need to extend them. This means, if you would like to check whether an integer has a valid value or not (I know we already have methods like TryParse), you can simply create an extension method and that will be available for all integer types that you declare. Let’s discuss with an example.

Create a new class library and add a class named CustomExtensionMethods.  It doesn’t matters what the class name is, but its the syntax of writing the extension method that matters. Next, we add a static method to check whether the integer value is greater then hundred or not. The syntax of an extension method requires that

  • Method must be static type.
  • First parameter of the function must be of the type on which this method is to be used, and must precede with this keyword.

Even if you check out the definition of the LINQ extension methods, you can see that they have the same syntax.

Extension Methods in C#

Inside the function we can have whatever implementation we need. Since our method will be working on integer type variables, our first parameter will be of an integer type, preceded by this keyword. Inside the function, we can add our logic. Return type of the function will be boolean, based on whether the input was greater then 100 or not. So our extension method will be like the below :

Extension Methods in C#

And that’s it. Our function is ready to be used now. Compile the code and add the library reference in your code. Declare any integer value and you can see your extension method in the list of available methods for that type.

Extension Methods in C#

That was quite simple. So just create a smart library of yours and add as many extension methods as you would like to and use them in the code. So easy to use. The best part is that you can use these in cases, where you would like to add methods to an existing library, which you can’t extend by inheritance. Hope you enjoyed reading it…!!!

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