After a few topics on the deep end of the magic that happens inside the computer, this time we will move up the ladder slightly to demonstrate a higher level concept that was introduced with Visual C# 3.0 and Visual Basic .NET 9: Extension methods. We will be focusing on how to use this concept in C# only.
What are extension methods?
Extension methods are a compiler-level "syntactic sugar" that basically allow you to spot weld additional methods onto classes that already exist, and make them appear (via compiler voodoo) as if they were defined in the class to start with. The important thing that I would like to reiterate is that extension methods are implemented at the compiler-level.
What is the point of an extension method?
This is a good question, because at first you might not really know what to use this language feature for. As said before, extension methods allow you to bolt on additional functionality to an existing class and make that functionality accessible in the exact same way as if it were defined in the class to start with. They are mainly used to simplify operations on an object and to permit code re-use. It is also handy when you want to bold on functionality to a class of which you do not control the source code (for example from a 3rd party assembly).
For example, lets take the String class in the .NET Framework (System.String). For the sake of the example, let say that my program converts lots of strings to upper case, and then writes them out to the console. Assuming extension methods did not exist, I could implement this two ways:
1. Call System.String.ToUpper() and Console.WriteLine() each time I need this operation.
2. Create a static helper class (for example StringHelpers) and define a static method method that does the same as (1). Call the static helper method each time I need this operation.
Of course, this section would hardly be complete without a code sample:
Implementation 1:
Of course, this implementation does not permit code re-use. What if we decided to convert the string to lowercase in a later revision of the program? We'd need to change many places in the program!
Implementation 2:
This implementation is better because it provides the ability to re-use code and makes it easier to change the behavior (only need to modify a single location). However, it is not quite as elegant as an extension method.
Implementing an extension method.
Interestingly enough, an extension method in C# is very similar to our static class + static method described in Implementation 2 above. In C#, an extension method must be a static method inside a static class, however for each method that is to be an extension method, the first parameter is decorated with the "this" modifier - this is the clue to the compiler that it is an extension method. Additionally, the first parameter MUST be the type/class that we want to extend. I'll demonstrate this below, and we will stick with our example from above:
Now, here is the fun part where we get to use the magic of the compiler. We can call the extension method directly off our string instance variable, like this (continuing the example):
The compiler automatically passes the instance to the extension method, so we call the method without any parameters - and it looks just like the ConvertAndPrint method was defined in the System.String class to begin with! Behind the scenes, though, the compiler generates code that does exactly the same as Implementation 2 above. You will notice this is the case if you use a program like Reflector to inspect the generated IL of both examples (advanced users).
I am getting a "Cannot resolve symbol XXX", where "XXX" is the name of my extension method. What is going on?
I did not use namespaces in my examples above, however if you defined your extension method static class inside a namespace and are trying to call it from a different namespace, you must import the namespace (in C# this is done via the using statement) from which the extension method static class is defined.
Other mistakes include not using a static class or method, or forgetting the "this" modifier on the first parameter of the extension method.
Happy welding!
Edited by tompsonn - 9/1/12 at 10:21pm
What are extension methods?
Extension methods are a compiler-level "syntactic sugar" that basically allow you to spot weld additional methods onto classes that already exist, and make them appear (via compiler voodoo) as if they were defined in the class to start with. The important thing that I would like to reiterate is that extension methods are implemented at the compiler-level.
What is the point of an extension method?
This is a good question, because at first you might not really know what to use this language feature for. As said before, extension methods allow you to bolt on additional functionality to an existing class and make that functionality accessible in the exact same way as if it were defined in the class to start with. They are mainly used to simplify operations on an object and to permit code re-use. It is also handy when you want to bold on functionality to a class of which you do not control the source code (for example from a 3rd party assembly).
For example, lets take the String class in the .NET Framework (System.String). For the sake of the example, let say that my program converts lots of strings to upper case, and then writes them out to the console. Assuming extension methods did not exist, I could implement this two ways:
1. Call System.String.ToUpper() and Console.WriteLine() each time I need this operation.
2. Create a static helper class (for example StringHelpers) and define a static method method that does the same as (1). Call the static helper method each time I need this operation.
Of course, this section would hardly be complete without a code sample:
Implementation 1:
Code:
string example = "VB is better than Huddler";
Console.WriteLine( example.ToUpper() );
Of course, this implementation does not permit code re-use. What if we decided to convert the string to lowercase in a later revision of the program? We'd need to change many places in the program!
Implementation 2:
Code:
public static class StringHelpers
{
public static void ConvertAndPrint( string input )
{
Console.WriteLine( input.ToUpper() );
}
}
/* Usage */
string example = "VB is better than Huddler";
StringHelpers.ConvertAndPrint( example );
This implementation is better because it provides the ability to re-use code and makes it easier to change the behavior (only need to modify a single location). However, it is not quite as elegant as an extension method.
Implementing an extension method.
Interestingly enough, an extension method in C# is very similar to our static class + static method described in Implementation 2 above. In C#, an extension method must be a static method inside a static class, however for each method that is to be an extension method, the first parameter is decorated with the "this" modifier - this is the clue to the compiler that it is an extension method. Additionally, the first parameter MUST be the type/class that we want to extend. I'll demonstrate this below, and we will stick with our example from above:
Code:
/* You can name the class anything, but I like to name it with the class I am extending and suffix it with "Extensions" */
public static class StringExtensions
{
/* Note the "this" modifier before the first parameter */
public static void ConvertAndPrint( this string input )
{
/* We should always check for null in an extension method */
if ( string.IsNullOrEmpty( input ) )
{
return;
}
Console.WriteLine( input.ToUpper() );
}
}
Now, here is the fun part where we get to use the magic of the compiler. We can call the extension method directly off our string instance variable, like this (continuing the example):
Code:
/* Usage */
string example = "VB is better than Huddler";
example.ConvertAndPrint();
The compiler automatically passes the instance to the extension method, so we call the method without any parameters - and it looks just like the ConvertAndPrint method was defined in the System.String class to begin with! Behind the scenes, though, the compiler generates code that does exactly the same as Implementation 2 above. You will notice this is the case if you use a program like Reflector to inspect the generated IL of both examples (advanced users).
I am getting a "Cannot resolve symbol XXX", where "XXX" is the name of my extension method. What is going on?
I did not use namespaces in my examples above, however if you defined your extension method static class inside a namespace and are trying to call it from a different namespace, you must import the namespace (in C# this is done via the using statement) from which the extension method static class is defined.
Other mistakes include not using a static class or method, or forgetting the "this" modifier on the first parameter of the extension method.
Happy welding!
Edited by tompsonn - 9/1/12 at 10:21pm







