Introduction
In this article, we will take a look at what extension methods are and how to use them in .NET. Personally, they are one of the best things that have been introduced into the .NET Framework in terms of readability. I will take you through what extension methods are, how to create them (in C# and VB), then I will show you some of the extension methods that I have created (in C# only, conversion is for you to try).
Contents
Extension methods allow you to easily extend a type, such as an integer
or string
, without re-compiling or modifying the type. In essence, they are a type of static
(shared
in VB) method, but they are called as if the method is native to the type. Extension methods are available from the 3.5 version of the .NET Framework and can be implemented on any type in the .NET Framework or any custom type that you define.
One downside to extension methods is if that you create an extension method with the same name as another method in that type, the compiler will bind the method call to the native method, not any extension. An extension method is only called when there is no native method found.
Warning
If you declare an extension method on the type Object, you will effectively create the extension method for every type in the framework including but not limited to String, Integer and Lists.
The basic outline of creating an extension methods goes something like this:
- Create a
public static
class (module
in VB)
- Define functions that you wish to perform
- Make the functions an extension method
Following through a complete example, I will now demonstrate how to create an extension method that returns the first 3 characters of a string
. Using the list above, I must first create a static class
or module
:
public static class Extensions
{
}
Module Extensions
End Module
The next phase would be to write the function that we are going to need, which in this case is the following:
public static class Extensions
{
public string GetFirstThreeCharacters(String str)
{
if(str.Length < 3)
{
return str;
}
else
{
return str.Substring(0,3);
}
}
}
Module Extensions
Public Function GetFirstThreeCharacters(Byval str As String) As String
If (str.Length < 3) Then
return str
Else
return str.SubString(0,3)
End If
End Function
End Module
So far, we have done nothing special. The last phase is to make the functions' extension methods. It is slightly more complicated in VB, but not by much. I will deal with C# first.
To make our C# version of our function, we need an extension method to mark the function as static
(so that it can be accessed at any time without the need for declaring anything) and secondly, mark the first parameter with the this
keyword. This keyword basically tells the CLR that when this extension method is called, to use "this
" parameter as the source. See the following:
public static class Extensions
{
public static string GetFirstThreeCharacters(this String str)
{
if(str.Length < 3)
{
return str;
}
else
{
return str.Substring(0,3);
}
}
}
Now for the VB version. Instead of using the this
keyword, we need to do something slightly different. We need to mark the function with the System.Runtime.CompilerServices.Extension
attribute like so:
<System.Runtime.CompilerServices.Extension> _
Public Function GetFirstThreeCharacters(Byval str As String) As String
If str.Length < 3 Then
Return str
Else
Return str.Substring(0, 3)
End If
End Function
If you copy this code into any project, you should be able to call it like so:
String str = "my new String";
str = str.GetFirstThreeCharacters();
Dim str as String = "my new String"
str = str.GetFirstThreeCharacters()
As I explained for both languages above, the effective use of the this
keyword makes the CLR take whatever we are calling the extension method from as the first parameter to our function.
Hint: Try adding an additional Integer
parameter and using that as a replacement for the 0 in the code above.
Here are a few of the extensions that I have found or created over time. These are helpful to me and I hope they are to you as well. If you have a question about any of these, drop me a comment below.
HasElements
Something that I often do is check a collection for a value. This method is designed to prevent me constantly checking for a null
value and existence of any item in a given collection. This method will work on any collection that implements the ICollection
interface.
Definition:
public static bool HasElements(this ICollection items)
{
return items != null && items.Count > 0;
}
Example usage:
List<String> myList = new List<String>();
if (myList.HasElements())
{
}
IsBetween
The IsBetween
method returns a boolean and determines whether or not a value is between an inclusive upper and lower boundary. This will only work on types that implement the IComparable
interface.
Definition:
public static bool IsBetween<T>(this T value, T low, T high) where T : IComparable<T>
{
return value.CompareTo(low) >= 0 && value.CompareTo(high) <= 0;
}
Example usage:
Int32 myInt = 0;
myInt.IsBetween(0, 5); myInt.IsBetween(1, 5);
Each
Quite often, I have to perform a task on a collection of items. This is just a shortcut way for saying for each element in the collection, perform this action. This will work on any collection that implements the ICollection
interface. The action that is parsed in can be a lambda expression or a function/subroutine.
Definition:
public static void Each<T>(this ICollection<T> items, Action<T> action)
{
foreach (T item in items)
{
action(item);
}
}
Example usage:
List<String> myList = new List<String>();
myList.Each(el =>
{
el.Substring(0,1);
el = el;
});
In
Often it is necessary to determine whether a value is in a set collection. For example, I need to check whether a string
is in an allowed list. This method will allow us to check any value against an array of values of the same type.
Definition:
public static bool In<T>(this T source, params T[] list)
{
if (null == source) throw new ArgumentNullException("source");
return list.Contains(source);
}
Example usage:
Int32 myInt = 0;
myInt.In(0, 0, 1, 2, 3); myInt.In(1, 5, 6, 7, 8);
Hopefully, you now have an understanding of how to implement extension methods in both C# and VB.NET. If you need any help with your extension methods or if you would like to ask a question, drop me a comment below.
History
- 28th September, 2011: Initial version
- 29th September, 2011: Fixed a compiler bug in the article's text. Source code is fine.