Hi,
This post summarizes Extension Methods, new feature provided by .net framework 3+ / ORCAS.
As name suggest Extension Methods are like you extends the existing features provided to the primitive data types. Like int data type provides you int.ToString() method, that converts the int to string, you can also add your own feature to int type.
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.
How to do it:
As stated above, you can add new feature to int type, we will see an example of Extension Method.
Suppose you want to check whether the int is even or odd number, below Extension Method checks whether the number is even or odd and returns true or false.
namespace TemplatesCl { public class ExtensionMethod { public static bool IsEvenNumber(this int value) { return (value % 2) == 0 ? true : false; } } } |
Above method “IsEvenNumber” is an extension method to the int data type, which returns whether the number is an Even or Odd number.
Below is an example to use this extension method.
Extension method are easy to use, so when I hit the "." keyword on a int variable, my extension methods will now show up in the intelligence drop-down list.
public string TestExtensionMethod(int num) { if (num.IsEvenNumber())// LINE A { return "Provided number is Even"; } else { return "Provided number is Odd"; } } |
As you can see from the above snippet, the differences between the extension method (IsEvenNumber):
1. Extension methods have the keyword this before the first argument, while static methods do not have the this keyword in its argument declaration.
2. When extension methods are consumed, the argument that was declared with the keyword this is not passed. In the above code, Line A is an example of consuming the extension method IsEvenNumber. No argument is passed to it. When static methods are consumed, no arguments are skipped.
3. Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.
4. Extension methods can be called only on instances values.
Extension methods, though static in nature, can be called only on instances. Trying to call them on a class will result in compilation errors. The class instances on which they are called are determined by the first argument in the declaration, the one having the keyword this.
Thanks,
Paresh Bhole
No comments:
Post a Comment