Implementing and executing C# extension methods

One of the useful features added to c# 3.0 is the introduction of Extension methods. It allows you to attach methods to sealed classes or classes that you do not have access to.

Let’s say you are working with a lot of numbers in your application and often have to check whether the numbers you are working with are even or uneven. What we are used to do is creating a helper class to extend a certain class like this:

C# Extension Methods

And we call this method by Extensions.IsEven method to do the validation whether the value is even or not:

C# Extension methods

One of the features C# 3 adds is that you can add these extension methods directly to the class you are working with. We are working with an integer, and it would be great if we could invoke the IsEven() method directly from the integer and not doing it through a Helper class. One of the advantages is that someone else in your project also doesn’t have to go dig through all the helper classes to see what methods exist in there to know what possible methods there are.

Changing the method to an extension method is a small change:

C# Extension methods

You add the this keyword before the value you pass on. To be able to work with extension methods the class the extension method belongs to has to be a static class and the extension method has to be a public static method. The first parameter of the extension method specified the type the method operates on and is marked with the this keyword. If doing so, you will be able to invoke the IsEven method directly from the int class:

C# Extension methods

The IsEven method is now directly accessable through the int variable, as where this was not before. Anyone working in this code working with an integer, would be able to find this IsEven method in his intellisense when coding, even if he does not know about any helper classes. Note you do not have to pass the integer value into the method as we used to do with the helper class method. Since we put “this int value” our extension method which is invoked on the integer variable, knows the value it needs is at the variable we execute this method on which means we do not have to pass the value into the method anymore.

And our code executes as we expect it to:

C# Extension Method

One of the important things to know is that the extension methods are only visible if the code you are writing belongs to the same namespace as the class the extension method belongs to or if an import has been done importing the namespace of the class which the extension belongs to.

In our case, both the extension class as the console code belong to the same namespace. Extension class:

C# Extension Methods

Console application code:

C# Extension Methods

If we now change the namespace of the Extension class from CSharp.ExtensionMethods to CSharp.ExtensionMethods.Extensions:

Now we will get an error in our console application code that the method IsEven is not known:

C# Extension Methods

We can solve this by importing the namespace of our extension class which will attach the extension method to our integer class:

C# Extensions Method

Any suggestions, remarks or improvements are always welcome.
If you found this information useful, make sure to support me by leaving a comment.

Cheers and have fun,

Robbin

About these ads