How To Use Extension Method In C#

Extension method is a new feature added in C# 3.0. An Extension method is used to add new behaviors to base type without changing the base feature. Extension methods can be attached to any other existing class without changing that particular class. Using extension method you can add a method of existing type or modify the original. Basically, Extension methods are static methods.

Key Points of Extension Method

  1. Extension method is a static method that must exist in static class.

  2. It always use “this” keyword with first parameter in the parameter list. Basically this keyword tells to the compiler that what class you have to extend. You can also include other parameter proceeding with it.

  3. You cannot override base method of existing type using an extension method. If you do this, the compiler will not give you an error but it will not work.

  4. If you create an extension method with same name as base method but different parameters then the extension method will call first not base method.

  5. If you create an extension method with same name and same signature as an instance method has then extension method will never be called because compiler take this as low priority method.

  6. You cannot use an extension method with properties, events or fields.

  7. If an extension method with same name and signature exist in two or more than two different namespace and these namespaces are included with a class where you want to use extension method, then compiler do not give any error. But it will give an error when you will try to call one of the extension method from them.

  8. Extension method’s namespace should be included when you use. You can import namespace of the class to use “Using” statement.

extension Method

Example 1

  1. public static class StringHelpers  
  2. {  
  3.   
  4.     public static string[] GetUserFirstAndLastName(this string name)  
  5.     {  
  6.         var firstName = string.Empty;  
  7.         var lastName = string.Empty;              
  8.         var len = name.Split(' ').Length;  
  9.   
  10.         string[] names = name.ToString().Trim().Split(new char[] { ' ' }, len);  
  11.         if (names.Length == 1)  
  12.         {  
  13.             firstName = names[0];  
  14.             lastName = "";  
  15.         }  
  16.         else if (names.Length == 2)  
  17.         {  
  18.             firstName = names[0];  
  19.             lastName = names[1];  
  20.         }  
  21.         else  
  22.         {  
  23.             firstName = names[0];  
  24.             lastName = names[names.Length - 1];  
  25.         }  
  26.         string[] firstlastName={(firstName),(lastName)};  
  27.         return firstlastName;  
  28.     }  
  29. }  
In this example, I have created an extension method which return a string array with firstname and lastname from pass name. Here I am going to show how to use this method.
  1. var name="Mukesh Kumar Singh";  
  2.   
  3. //GetUserFirstAndLastName is an extension method.  
  4. string[] firstlastName = name.GetUserFirstAndLastName();  
  5.   
  6. var FirstName = firstlastName[0].ToString();  
  7. var LastName = firstlastName[1].ToString();  
Example 2
  1. public static class StringExtensionMethod  
  2. {  
  3.     public static string[] SplitByComma(this String s)  
  4.     {  
  5.        return s.Split(',');  
  6.     }  
  7. }  
  8. class Program  
  9. {  
  10.     static void Main(string[] args)  
  11.     {  
  12.       string input = "string1,string2,string3";  
  13.       var result = input.SplitByComma();  
  14.       Console.WriteLine(result[1]);  
  15.     }  
  16. }  
Today we learned what is an extension method in C# and how to use it. We also learned the main key points about an extension method.

Thanks for reading this article, hope you enjoyed it.

 


Similar Articles