When you read the word extension. What is the first thing that comes to your mind? Probably it will be Chrome or Firefox plugin that alters (aka. extends) the ways web browser works. Plus, we are going for the extensions because we can’t change the code of a web browser by our own. This bears a resemblance to the extensions we can create in programming. Let’s see how.
If you want to download the code, it can be found at: https://github.com/Nivo1985/AdvancedCSharp. The project names match the blog post topic.
What, why and when?
First what. I’m going to use definition provided by the Microsoft. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type. For client code written in C#, F# and Visual Basic, there's no apparent difference between calling an extension method and the methods defined in a type.
Now why. We should be using extension methods if we want to extend to behaviour of a type. And we believe that the best place for method in development is directly inside the type.
Finally, when. We should do it when we don’t have direct access to the type we need to extend. Although, we can extend the typed we are developing, there is no gain in that. On the other hand, the side effects of that can be unintentional code obfuscation. And we don’t want that 😉
We will start simple. By extending the types we are well familiar with. The place for storing the extension method will be BlogExtensions.cs file. Now the class looks like this.
There are three key elements required for extension method. One, it needs to be placed in public static class. Two: it needs to be public static itself. Finally: the first parameter of the extending method needs to be this and type that we are extending.
In the example above we can see two extension methods being defined. First one is called InsertSpaces. It is an extension of type string. It operates on a string it has been called on and returns a string with underscores inserted after each original character.
The second method SumWith extends integer. In this case the extension method takes one extra parameter and returns a string. Running the code that uses the extension methods doesn’t differ from calling any other method. We can test these two new methods in Program.cs file. The code for test looks like this and give us these results.
The results shouldn’t be a surprise. Both of developed extension methods worked in the way we wanted. Now to something more complex with extending the custom class.
To extend the class we need to have a class. Shocker, I know. The class we will be working with looks like this.
This is a very simple class. It has only two properties, a constructor and two methods. The only goal of this methods is to demonstrate how they will work with extension methods. That being said let’s add three extension methods. To do that we will stick with the BlogExtensions class. Now, the class looks like so.
There are three extension methods extending the Person class. They look very similar but there is a difference in a way they fit into class design. GetIntroduction is a new method in the class design. GetIntroductionPublic is an extension method with the same name as the class inner public method. GetIntroductionPrivate is an extension method with the same name as the class inner private method. No we will run all on this three methods and see what will happen. The code to run them looks like this and produces this results.
The results are giving us a good insight into how the process works. In first case the method from the extension class was called. Sure, it was, there was no other option to choose. In second case the inner public method was executed. In the third case we still can’t use class private method so the public extension method is the one that is being used. Those three examples are worth remembering if you plan on using extension methods in your code.
It is a jungle out there
At this point you should be asking yourself a question. Are extension methods being used in .Net ecosystem. Before I answer this question, please look at this piece of code.
In this super simple example, we are taking first or default element from the list of Person class objects and call GetIntroduction on this object. The interesting thing about this example is the fact that FirstOrDefault is an extension method itself. You can check that is you go to the definition of this method. If you do so you will see this piece of code.
And wouldn’t you know. FirstOrDefault is an extension method. That answers the question of extension methods real world usage. Extension methods are being used heavily in .NET environment. Very often in Nuget packages.
The code above has one more element that I would like to dive deep into. That is generic code in extension methods.
Generic extension methods
As you have seen above, we can create extension method that uses generic types. So, let’s modify BlogExtensions class for the last time. Now the class looks like so.
At the bottom of the code, we can see a newly added method of Search. It is a generic method that returns a generic IEnumerable. Methods second parameter in very interesting. The method expects a predicate. That predicate is to take the generic type as a parameter. Looks familiar? Good, it should to. It was written in LINQ fashion. Usage of this method is also very like usage of LINQ methods. Just take a look.
The usage of the new Search method just like any “other” LINQ method. That was our goal. Now you see how easy it is to extend the code.
This was the fourth article in the advanced c# series. We have talked about extending existing types. We have seen that this technic is being widely used and give us huge range of possibilities. But it needs to be used with cautions.
If you have any questions, please drop me a line at email@example.com.
Till next time. Keep coding.