Extension methods are static methods, which are called as if they were instance methods on the extended type. With Extension methods, you can add methods to existing types without even creating a new derived type, recompiling, or modifying the original type.
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is introduced in C# 3.0.
C# allows us to create and add new methods to existing class without creating a new child class. The existing class does not require recompiling the code. C# extension methods are the special type of the static methods that can be called as instance methods.
We can add extension methods in both C# predefined classes and user created custom classes. We need to consider the following points to define an extension method.
An extension method should be a static method.
It must have this keyword associate with class name.
The class name should be the first parameter in the parameter list.
Steps to create an Extension Method
Step 1: Define a static visible class which will contain the Extension Method or Extension Methods. Make sure the class is visible to the client code by applying the appropriate access modifier.
Step 2: Create a static method with at least the same visibility level as the containing class.
Step 3: The first parameter of the Extension Method always specifies the type that the method operates on. Make sure the type name is preceded with the āthisā modifier.
Step 4: In the calling code, add the namespace that contains Extension Method class.
Step 5: Use the Extension Method on the type in the same instance method can be used. Keep in mind that we do not need to pass the first parameter because that denotes the type, however we should pass the second parameter onwards to call the extension method.
Example:
using System;
namespace CSharpFeatures
{
public static class StringHelper
{
public static string GetUpperCase(this string name)
{
return name.ToUpper();
}
}
public class ExtensionMethodExample
{
static string name = "This is the Example of Extension Method in C#";
static void Main(string[] args)
{
string str = name.GetUpperCase(); // calling extension method using string instance
Console.WriteLine(str);
}
}
}
Important:
An extension method must be defined as a static method and must reside in a static class
this keyword must be the first parameter of the extended method. This will actually inform the compiler that the method is an extension method.
An extension method must be defined in the root of the namespace. It cannot be nested inside another class under the namespace
The type that is being extended must be referenced in the extension method. Use the using statement to import the namespace.
protected/private members of the type that is being extended will not be accessible from the Extension methods.
You cannot override any method of the type you are extending.
The Extension method has lower priority than the methods defined In the type. A similarly named method in an instance, always executed rather than the extended method. In fact, the extended method will never be called
The Extension methods with the same name and signature may signature for the same class may be declared in multiple namespaces without causing compilation errors.
Extension method cannot be used for adding a new properties, fields or events
Advantages:
The main advantage of the extension method is to add new methods in the existing class without using inheritance.
You can add new methods in the existing class without modifying the source code of the existing class.
It can also work with sealed class.
The Tech Platform
Comments