At first, lets have a quick overview of this tutorial:
- Introduction
- What are extension methods?
- Creating Extenstion Methods
- Using Extenstion Methods
- Known problems
Lets get started!
1) Introduction
Let's assume you want to convert an integer value to a hex value and print it out. Your code may look like this:
//...
int i = 15;
string hex = String.Format("0x{0:X}", i); // get hex value and convert to string
Console.WriteLine(i.ToString()); // output: 15
Console.WriteLine(hex); // output: 0xF
//...
Looks already quite simple, doesn't it? But...
In line 3 of this little code snippet the integer i is converted to a string by using .ToString(). Wouldn't it be nice if we could just write
//... Console.Writeline(i.ToHex()); //...
As we cannot change or add a new method to the Integer Class, one pretty nice and clean solution is the use of extension methods.
(.ToString() is in fact not an extension method. )
2) What are extension methods?
Extension methods are external methods which feel like they are part of a specific class (in our example: the Integer Class).
They are actually "just" syntactical sugar for a better/easier code handling and to make your code look cleaner.
3) Creating extension methods
Firstly, create a new class File. I usually call it something like "IntegerExtensions" or just "Extensions".
The keyword static in the class signature is required as extension methods have to be in static classes as static methods. And it must be public.
Your code should now look like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace ExtensionMethod
{
public static class IntegerExtensions
{
}
}
Now let's define the signature of the extension method we want:
public static string ToHex(this int i)
{
}
The method is pulbic and static. And it's return type is defined as a string (to use it in Console.Writeline()).
The first parameter must be the type of object that we are creating this extension method for - marked with this.
After this there can follow as much parameters of any type as needed.
The complete implementation should look like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace ExtensionMethod
{
public static class IntegerExtensions
{
public static string ToHex(this int i)
{
return String.Format("0x{0:X}", i);
}
}
}
That's it! Now we can use our new int extension.
4) Using extension methods
Well, to convert an integer value to a hex value is now very, very simple:
//... int i = 15; Console.Writeline(i.ToString()); Console.Writeline(i.ToHex()); //...
5) Known problems
You may get the following.NET version error:

There is a simple solution for this problem. Insert the following code in the Extension.cs outside your namespace:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute { }
}
Here is a nice article about "Using Extension Methods in .net 2.0"
Thats the end of this tutorial.
It should gave you a quick overview of the opportunities of Extension Methods in C# - creation and useage.
This post has been edited by LarcaCode: 13 February 2013 - 01:48 PM




MultiQuote






|