Please enable Javascript to correctly display the contents on Dot Net Tricks!

C Sharp Extension method

 Print 
  Author : Shailendra Chauhan
Posted On : 19 Jun 2012
Total Views : 100,471   
Updated On : 28 May 2014
 

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. At compile time an extension method call is translated into an ordinary static method call.

A simple Extension Method Example

//defining extension method
public static class MyExtensions
{
 public static int WordCount(this String str)
 {
 return str.Split(new char[] { ' ', '.', ',' }).Length;
 }
} 

class Program
{
 public static void Main()
 {
 string s = "Dot Net Tricks Extension Method Example";
 
 //calling extension method
 int i = s.WordCount();

 Console.WriteLine(i);
 }
}
//output:
6

Extension Methods Chaining

Like as instance methods, extension methods also have chaining capability.

 public static class ExtensionClass
{
public static string Pluralize (this string s) {...}
public static string Capitalize (this string s) {...}
}
//we can do chainig of above methods like as
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.

 static class StringExtension
{ // first method
 public static bool IsCapitalized (this string s) {...}
}
static class ObjectExtension
{
 // second method
 public static bool IsCapitalized (this object s) {...}
}
// code here
// first method is called
bool flag1 = "Dotnet-Tricks".IsCapitalized(); 
// second method is called
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.

Summary

In this article I try to explain extension method and its various use with example. I hope after reading this article you will be able to use this trick in LINQ query. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS
24 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
23 SEP
MEAN Stack (Classroom)
8:30 Am To 11:30 Am
23 SEP
Microsoft Azure Infrastructure Solutions (Online)
08:00 PM-11:00 PM IST / 9:30 AM-12:30 PM CST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
7 SEP
ASP.NET MVC with Angular4 (Online)
09:00 PM to 11:00 PM IST (+5:30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
04:00 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
11:30 AM - 1:30 PM IST (+5:30GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)
LIKE US ON FACEBOOK
 
+