Extension Methods

Introduction

Extension methods are a great feature of C# that was added in C# 3.0, but many people that I have talked to did not know about them or what to use them for.  Extensions methods allow you to add a new method to a class.  Another way to do this same this is to derive a new class from the original class and just add the new method.  A new derived class works in make cases, but this will show times where that is not possible, but where extension methods work.

Starting Code

For this example, I am going to start with a very simple class to represent a rectangle.  This could be something like the dimensions of a room in a game or map, or it could be something that we are just drawing on the screen.  The new class will have a Height and Width and that is all we need to start with it.  The Main method for the console application will create a variable of this type, and set its values.  It is a pretty basic example to start with.

Derived Class and Extension Method

Derived Class

The standard way that people have added functionality to a class is to derive a new class from the original and then add the new methods to the new class.  This will work, however it is not without issues at times.  The first is that if you are adding a new class and a new method and have a large code base (100k lines) where you use the original class in many files, all of the references to the class might need to switch to the new derived class.  Visual Studio will let you change all references to the class to a new name if you changed the name of it, but it really cannot just change all of your references to the new class.  Also, you might not want to convert all of the references to the new class name.  This means finding each one and changing it manually.  Visual Studio will help with this, but it is still very manual.  So adding a new class and a new method to our original class would look like this:

Notice that in the code above, the Main method had to change the reference to the class that was created.  This is a small example of what I was talking about above.

Extension Method

Extension methods can do something that is very similar and let you add a new method to a class without deriving a new one.  You have to make a new static class for this to work.  The new method will also be static as well.  We will never create the class itself, but it is used to hold the definition of the extension method.

The original class and Main method are not really changed, just the new static MyExtensions class will now how a new method for the original class.

Sealed Classes

All of this is great, but some classes, such as the .NET String class, are sealed.  A sealed class cannot be derived from to make a new class, but you can create an extension method on a sealed class.

This allows developers to add functionality to a class where they have no source code, but it make logical sense to add the feature to the class.  I have known a few developers that would actually create their own Point classes because the existing one lacked a single feature and it was sealed.  Extension methods would have solved their problems.

Parameters

Extension methods can also have additional parameters that can be passed into the method call.  This can be done by adding an additional parameter after the first one that describes the class that is being extended.

Issues

One of the biggest issues that I have seen is that the extension methods and the classes that hold them can be hard for people to find in the source code.  The simplest way to find the code is to right click on the method and then go to the definition of the method or use F12 when the cursor is on the method.  The problem is that people might not know that the method is an extension method by looking at the code only.  The tooltip for the method will also show the fact that it is an extension method.

extensiontooltip

extensiondefinition

Conclusion

Extension methods can add new methods to classes for you to add new functionality.  The original class does not even need to have source code available to extend it with the new method.  This can help add functionality to classes even if they are not developed in-house.  As I said earlier, this is a feature that has been available since C# 3.0 but I have known many .NET developers that have never known about this because they worked around the issue in other ways.  It is always good to know about a feature that could be useful for you sometime in the future than to have to recreate the wheel by hand.

Leave a Reply

Your email address will not be published. Required fields are marked *