There are many cool features available in C# 3.0 vis
- Extension Methods
- Implicitly Typed Local Variables
- Lambda expressions
- Object Initializers
- Anonymous types
- Implicitly typed arrays
- Query expressions
- Expression trees
Out of these cool features, I would like to write about Extension Methods first. I would write about C# 3.0 features in my subsequent articles.
Most of the times we tend to code in our traditional way and ignore such a cool features which are available to make developer’s life easy. For example, a developer like me who mostly worked on Framework 2.0 will hardly dare to spend more time in trying new alternate available way, in care of dead line J I would suggest be familiar with new features, trying it in dummy samples and be ready to use in your real projects.
Before heading towards Extension methods… let’s consider we have to create a method to reverse a string. So our usual approach would be to write a method accepting a string parameter, writing logic to reverse a string and returning a reversed string. Below is the code, we might have coded for this requirement.
class Program
{
static void Main(string[] args)
{
String myString = "Sanjivani";
Program obj = new Program();
String reverseString =obj.Reverse(myString);
Console.Write(reverseString);
Console.ReadKey();
}
string Reverse(string str)
{
char[] arr = str.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
Well, now heading towards Extension Methods…
Like name suggest, Extension Methods are extensions to typed instances. Extension Methods are static methods that can be invoked using instance method. For example when we have string type variable we get many built in methods and properties like...
strObj.Replace()
strObj.Split()
strObj.SubString()
ect
In the same way we can make our Reverse method available like strObj.Reverse(). So Reverse method will be available for all instances which are of string type.
Like we have Utility or common classes for our projects, we can have extension library having methods which works on a type.
Implementing Extension Methods:
Let’s convert same Reverse method into an extension method. I created new class for all extension methods as below:
public static class ExtensionLib
{
public static string Reverse(this string str)
{
char[] arr = str.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
If you notice, the changes I have method to the function are:
1) Introduced this before first parameter. First parameter’s type will always tell compiler that, that extension method is going to operate on the type used for first parameter.
2) Added public and static modifiers to Reverse method
3) Note that new class also has public and static modifiers
You are ready to use this extension method now:
class Program
{
static void Main(string[] args)
{
String myString = "Sanjivani";
Console.Write(myString.Reverse());
Console.ReadKey();
}
}
Please note that, my Extension method class and the program where I wanted to use this extension method were in same namespace, so I did not add extension method namespace in my Program class. But if both are in different namespaces, you will have to add extension method class namespace in your client program.
Your comments and knowledge on the topic are always welcome. I will appreciate your contribution :)
Comments