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
- Extension method is a static method that must exist in static class.
- 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.
- 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.
- 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.
- 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.
- You cannot use an extension method with properties, events or fields.
- 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.
- Extension method’s namespace should be included when you use. You can import namespace of the class to use “Using” statement.
Example 1
- public static class StringHelpers
- {
-
- public static string[] GetUserFirstAndLastName(this string name)
- {
- var firstName = string.Empty;
- var lastName = string.Empty;
- var len = name.Split(' ').Length;
-
- string[] names = name.ToString().Trim().Split(new char[] { ' ' }, len);
- if (names.Length == 1)
- {
- firstName = names[0];
- lastName = "";
- }
- else if (names.Length == 2)
- {
- firstName = names[0];
- lastName = names[1];
- }
- else
- {
- firstName = names[0];
- lastName = names[names.Length - 1];
- }
- string[] firstlastName={(firstName),(lastName)};
- return firstlastName;
- }
- }
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.
- var name="Mukesh Kumar Singh";
-
-
- string[] firstlastName = name.GetUserFirstAndLastName();
-
- var FirstName = firstlastName[0].ToString();
- var LastName = firstlastName[1].ToString();
Example 2
- public static class StringExtensionMethod
- {
- public static string[] SplitByComma(this String s)
- {
- return s.Split(',');
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string input = "string1,string2,string3";
- var result = input.SplitByComma();
- Console.WriteLine(result[1]);
- }
- }
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.