Objectives:
- What is Extension Method
- How to create Extension methods
- How to use Extension Method
Extension method is a new feature added in C# 3.0. It is a mechanism of adding new methods into an existing type (class or struct) without modifying the existing type. The original type is not re-compiled when extension methods are added to the type. As code recompilation is not needed, it is major advantage of Extension method.
Extension methods in C# allow you to add new methods to existing types without modifying the original type or creating a new derived type.
But the question is why Extension method was added in C# version 3.0.
Reasons for Extension method in C#
Suppose that class A has some methods and the class is not sealed class. In future, if the business requirement changes then we can add new methods in its derived class (say class B is derived class) as per the business need. So, we can extend the functionality of the class using inheritance without making any change in parent class A.
- But there can be problem if the parent class is sealed class. In this case, we cannot inherit the class. It means business requirement cannot be met without changing the code of method of original class.
- Another related problem can be: if the original type is not a class but struct. In this case also, we cannot inherit the original type. Thus we find the OOP Design Open Closed Principle is violated if original class is changed or business needs cannot be fulfilled.
Considering these problems, C# team invented Extension method as language construct.
Steps To create an Extension method
- Create a static class which will define all extension methods. In a static class, all methods must be static methods.
- Create a static method as extension method in this class. You can create one or more extension methods in this static class.
- Now the task is to convert a static method into extension method: To convert a static method into extension method, the type of first parameter of the method must be of the target type/class/struct. Plus, the data type of first parameter must be prepended with this keyword. For example, if you are creating an extension method for Employee class then the signature of Extension method will be as follows: ExtesnionMethodName(this Employee arg1, Type arg2, ...) The this keyword indicates that the static method will behave as instance methods of the target class (e.g. Employee). Basically this keyword and target type parameter bind the static method of the static class with the original type.
- Write the business logic inside the body of extension method.
Features of Extension Method
- It is defined as a member of static class.
- It is a static method which first parameter's type is the type for which extension method is being defined; also this first parameter must be prefixed/qualified with this keyword.
- It is invoked or called on the instance of the type (as if the this static method is an extension method of the type).
Here's how you can create and use extension methods in C#:
1. Create a static class for extension methods: First, create a static class to contain the extension methods. The class should be static, and the methods should also be static. The class should be in the same namespace as the types you want to extend.
using System;
public static class StringExtensions
{
public static string Reverse(this string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
In this example, we're creating an extension method called Reverse for the string type.
2. Define the extension method: The extension method is a static method defined in the static class. It takes the type to be extended as the first parameter, prefixed with the this keyword.
3. Using the extension method: You can then use the extension method on instances of the extended type.
using System;
class Program
{
static void Main(string[] args)
{
string originalString = "hello";
string reversedString = originalString.Reverse();
Console.WriteLine("Original: " + originalString);
Console.WriteLine("Reversed: " + reversedString);
}
}
In this example, we're calling the Reverse extension method on a string instance.
4. Output: The Reverse extension method modifies the behavior of the string type without modifying the original string class.
Original: hello
Reversed: olleh
Remember to include the namespace where the extension method class is defined (if it's in a different namespace) or import the namespace to use the extension methods.
In summary, an extension method
- extends the type of its first parameter
- is defined as a static method with a this modifier on its first parameter
- must be defined in a static class
- cannot access private instance variables of its extended class
No comments:
Post a Comment