Extension methods have been available in the .NET Framework since 3.0. If you haven’t created one before, the chances are very high that you have at least used one, possibly without realizing it. In this post, we will explore what extension methods are, how to create them, and compare them to regular class methods.
What are Extension Methods?
Extension methods are methods that extend the functionality of a class. They do not have to belong to the class or the library of the class they extend and can be created by anyone for just about any class available. This gives us a great amount of power to make objects in the .NET Framework and external libraries more useful to us. We can even create them for our own classes when it makes sense.
How do Extension Methods Differ From Normal Methods?
Extension methods appear almost identical to methods already in the class to consumers with one small caveat. That caveat is that you must reference the namespace of the extension method before it can be used. Realistically, however, extension methods are quite different than normal methods in a class.
Extension methods and the class they exist in must be static. Extension methods also must have at least one argument: the object they are extending. However, you don’t have to pass that object in. Before I confuse you, let’s take a look at a quick example.
Listing 1.1 – A sample extension method
public static class ContactExtensions { public static string FullName(this Contact contact) { string fullname = string.Empty; fullname = contact.FirstName; if(!string.IsNullOrEmpty(fullname)) { fullname += " "; } fullname += contact.LastName; return fullname; } }
Both the ContactExtensions class and the FullName method are static. The first and only argument in the FullName method is a Contact argument but note the this keyword. It is the type we are operating on and it is what makes extension methods possible. We don’t have to supply it and it provides the context for the FullName method. Now, let’s look at a quick implementation.
Listing 1.2 – A simple implementation
Contact contact = new Contact { LastName = "Galt", FirstName = "John" }; Console.WriteLine($"First Name: {contact.FirstName}"); Console.WriteLine($"Last Name: {contact.LastName}"); Console.WriteLine($"Full Name: {contact.FullName()}");
See that the call to contact.FullName is a method call but that we don’t have to supply the Contact argument. It just seems like a method already existing on the Contact object.
Listing 1.3 – Output
First Name: John
Last Name: Galt
Full Name: John Galt
You can, however, have other arguments available for an extension method. Those arguments may also be given a default value but the normal rules apply there.
Listing 1.4 – An Extension Method With an Additional Default Argument
public static string DisplayName(this Contact contact, string separator = ",") { string displayname = string.Empty; displayname += contact.LastName; if(!string.IsNullOrEmpty(displayname) && !string.IsNullOrEmpty(contact.FirstName)) { displayname += separator; displayname += " "; } displayname += contact.FirstName; return displayname; }
In this example, the extension method has a second argument but supplies a default value for that argument. And we can call the new extension method easily with: Console.WriteLine($"Display Name: {contact.DisplayName()}");
.
Listing 1.5 – Output
First Name: John
Last Name: Galt
Full Name: John Galt
Display Name: Galt, John
Conclusion
Extension methods can be very handy when used with objects outside of your own control. I hope that adding this tool to your belt will allow you to create a better experience for yourself and your colleagues.