Wednesday 14 August 2013

C# Extension method

An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviors to an existing type without altering. In extension method "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by extension method.
Conceptually, extension methods provides as an implementation of the decorator structural pattern.
  1. public static class ExtensionClass {
  2. public static bool IsCapitalized (this string s)
  3. { if (string.IsNullOrEmpty(s))
  4. return false;
  5. else
  6. return char.IsUpper (s[0]); }
  7. }
  8. class Program {
  9. static void Main(string[] args)
  10. { bool flag;
  11. string str="Dotnet-tricks"
  12. if(str.IsCapitalized()) //invoking the extension method
  13. Console.WriteLine ("\n The given string is capitalized");
  14. else
  15. Console.WriteLine ("\n The given string is not capitalized"); }
  16. }
At compile time an extension method call is translated into an ordinary static method call. The translation of above static method will as follows:
  1. ExtensionClass.IsCapitalized (str)

Extension Methods Chaining

Like as instance methods, extension methods also have chaining capability.
  1. public static class ExtensionClass
  2. {
  3. public static string Pluralize (this string s) {...}
  4. public static string Capitalize (this string s) {...}
  5. }
  6. //we can do chainig of above methods like as
  7. string x = "Products".Pluralize().Capitalize();

Ambiguity, resolution and precedence of extension method

To use an extension method, it should be in same scope of class.If two extension methods have the same signature,the more specific method takes precedence.
  1. static class StringExtension
  2. { // first method
  3. public static bool IsCapitalized (this string s) {...}
  4. }
  5. static class ObjectExtension
  6. {
  7. // second method
  8. public static bool IsCapitalized (this object s) {...}
  9. }
  10. // code here
  11. // first method is called
  12. bool flag1 = "Dotnet-Tricks".IsCapitalized();
  13. // second method is called
  14. bool test2 = (ObjectHelper.IsCapitalized ("Dotnet-Tricks"));

Key points about extension methods

  1. An extension method is defined as static method but it is called like as instance method.
  2. An extension method first parameter specifies the type of the extended object, and it is preceded by the "this" keyword.
  3. An extension method having the same name and signature like as an instance method will never be called since it has low priority than instance method.
  4. An extension method couldn't override the existing instance methods.
  5. An extension method cannot be used with fields, properties or events.
  6. The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in same class file using directives. Compiler will cause an error if you will try to call one of them extension method.

No comments:

Post a Comment