Here’s What You Need To Know About .NET Extension Methods
Extension methods are, as the name suggests, a convenient way to extend a class or object’s behaviour by means of external extension. That is, without modifying the class or object directly itself. Extension methods are convenient because they can be used directly on the object or classes they are intended to extend and the extension does not need to be encapsulated in the object it is extending. This is in contrast to utility classes/methods that require you to pass the object in. Here are some guidelines to .NET extension method usage…
Extension methods should be used wisely!
Extension methods, if implemented in too many places at once can make code maintenance more difficult. Realise when is a good time to use an extension method and when you might be better off adding the behaviour directly to the object or class in question. Alternatively a utility class may still be a good choice.
Don’t spread extension methods far and wide
Minimise the amount of files you declare extension methods in. Having a single namespace or ExtensionMethods class will help logically group them together and make it quicker and easier to debug as they won’t be scattered throughout code. In the case of a development team, it may be a good idea to agree upon a core namespace or file to keep most extension methods in for easier code maintenance. Similarly, developers know where to place new extensions. Depending on the project, it may even be worth while agreeing on a core set of extension vocabulary.
Keep extension method usage short and simple
Good candidates for extension methods are those that have few or no parameters. Create well defined behaviour on the object or class. Simple conversion operations, validations etc. are good examples.
// Example validation extensions string emailAddress = "joe.bloggs@fakemail.com"; // Extension method on the string class emailAddress.IsValidEmailAddress(); string phoneNumber = "03 1234 4567"; postcode.IsValidPhoneNumber();
// Example conversions Person person = new Person { FirstName = "Joe", LastName = "Bloggs", EmailAddress = "joe.bloggs@fakemail.com" }; // Extension methods to the Person class. string personAsCSV = person.ToCSV(); Person p = Person.FromCSV(personAsCSV);
Use extension methods to leverage natural behaviour discovery through intelli-sense
One great thing about most common IDE’s these days is out of the box support for intelli-sense. If you are using Visual Studio, hitting ‘Ctrl’ + ‘.’ on an object will bring up it’s method list. This allows a developer to readily discover extensions on the object. This is a big advantage over utility classes or behaviour encapsulated outside the object where if you don’t know they exist, you will likely not use them.
Extension methods have lower precedence than an object or classes own implementation
What this means is that if you extend a class or object with an extension method and at some point in the future this class or object implements your extension, the class/object will no longer continue using your extension method and will opt to use its own method. This is something to be mindful of as you will be given no indication by the compiler that your extension method is not being called. This could be a subtle way to introduce an unintended bug.
Extension method benefits
The real benefit is in being able to extend classes or objects that you have not written or do not have access to the source code to modify directly yourself. This allows you to add functionality to a closed off version of code, third party libraries or even the .NET framework itself! Do you have any experiences you’d like to share with .NET extension methods?
