Extension methods in c# .Net Framework
By: Gonzalo P.
Tags: Tips, Software Development, Extension Methods, Good Practices, Tutorial

Extension methods in c# .Net Framework

Extending compiled classes

an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type

Extension methods are a very useful when you are developing applications with the .Net Framework. They give you the ability to add methods to classes that are already compiled and referenced in the project, so you don’t have to modify the original class to add a new method that solves something that is not included in that object.

When to use them?

The most common circumstance where you would need to use this, is when you need to do a very specific and small task that is applyable to an instance of an already compiled class, and this task will be used in more than one ocassion in the application. Think in some “function” that a already implemented class should have or you need that is not part of it. I will give you a very simple example.

Lets assume that you are in a situation where you need to evaluate if a integer variable called “number” is between other two integer numbers (“a” and “b”), you would code something like this:

Imagine that you need to do this same evaluation in a lot of places in the application. A lot of “if’s” right?. it would be great if the native type “int” had a method “isBetween” that evaluate this in a more simple and elegant way.

The int type don’t have this “inBetween” method, but…. you can make it yourself with the extension methods! (And without having to create your own “int” type or modifying the original implementation :D).

So, the first step to test this is create a new console project and add a “Extensions” folder where our extension will live. Then, create a IntExtension.cs file with a static class called “IntExtension” with a static method called “isBetween”. This method should have 3 int parameters and will return a boolean following the logic recently discussed:

But, this is just a class with a static method, how we transform it to an Extension method Easy! add the word “this” before the “int number” parameter. With this, you are telling to the method that he will be an extension of the int type and that he should pass the instance through the “number” parameter. It should look like this:

And for using this you need to add a “using” with the namespace of the class where the extension method is, and then just create a “int” variable and call the method:

A more complex situation

In a very recent work, I had to send a list of people (Partcipants) to a database. Every participant have a name, telephone number, email and a boolean that determines if that person was an administrator or not. The problem is that the Data Access Layer (DAL) only accepts basic types like strings, numbers, dates and arrays for each of those types. So, if you want to send a list of objects, you need to send arrays for each property of the object (One array of names, other for telephone and other for emails). And obviously, I needed to do this in a lot of places… every time I needed to save in the database a list of anything.

The solution?

Create a Method Extension for the IEnumerable interface that retrieves a Dictonary with all the arrays needed for each property of the class that is listing (the name of the property is the key of the dictionary). So, every time that I need to create the arrays based in a List<object>, just call the extension method:

Conclusion

I hope this helped you to understand how the extension methods works and when to use them. The full working example is in a repository in my GitHub account:

Also, you can read more about Extension Methods in the following links:

Please leave some comments if you have any doubt or sugestion about this post. It would be nice to have some feedback :)

Happy coding!

Gonzalo.