Basically Extension methods are a special kind of static method, but they are called as if they are instance methods on the extended type.
Extension methods enable us to attach methods to existing types without creating a new derived type.
The syntax of declaring an extension method is to
- Declare a static method in a static class
- And use this modifier to modify the first parameter of a static method.
- The type of first parameter will be the target type this extension method attaches to.
- When calling the extension method, we don’t need to provide argument for the first parameter.
Note
- Both the extension method and the host class must be static.
- In extension method, first parameter must be this
Let’s see an example.
Here I am going to create an extension method for rounding the value of double values.
Usually, we use Math.Round(double value, int digits) method to round the value with no of decimal values based on given digits passed in the method.
I am attaching this round operation to double type and make it look like a member method of double object.
First declare extension method as follows,
- static class DoubleExtension {
- public static double Round(this double value, int digits) {
- return Math.Round(value, digits);
- }
- }
After that we can use above extension method as shown below,
- class Program {
- static void Main(string[] args) {
- var a = 32.56829;
- var result = a.Round(2);
- Console.WriteLine(a);
- Console.WriteLine(result);
- }}
As we can see, here we created an extra method
Round(int digits ) to double type.There are two parameters when we create method, but when calling the extension method, we just need to give one argument, and first argument will be caller double value itself.Here we calling Round method as follows,
The extension methods can attach to only a specific type. Now let’s see how we can attach the same group of extension methods to multiple types using interfaces or abstract classes. When we attach extension methods to an interface or an abstract class, all derived classes will have these extension methods attached.
For example, first let’s create an interface called IDeveloper
- public interface IDeveloper {
- string Name { get; set; }
- string WorkDescription { get; set; }
- int Experience { get; set; }
-
- }
Next let’s create two types of developer classes implementing IDeveloper Interface as shown below
- public class FullStack : IDeveloper {
- public string Name { get; set; }
- Public string WorkDescription { get; set; }
- public int Experience { get; set; }
- }
-
- public class Android : IDeveloper {
- public string Name { get; set; }
- public int Experience { get; set; }
- Public string WorkDescription { get; set; }
- public void OtherFunction() { }
- }
To attach a group of extension methods to the IDeveloper interface, we will create a static class to extend the IDeveloper interface as shown below
- public static class IDeveloperExtension
- {
-
- public static void NameSpec(this IDeveloper dev)
- {
- Console.WriteLine("My name is "+ dev.Name);
- }
-
- public static void RoleSpec(this IDeveloper dev)
- {
- Console.WriteLine("My Skills are "+ dev.WorkDescription);
- }
-
- public static void ExperianceSpec(this IDeveloper dev)
- {
- Console.WriteLine("My Experience is "+{dev.Experience}
- +" years.");
- }
- }
After declaring the above extension methods for IDeveloper, we can write the below code to run
- static void Main(string[] args) {
- var fullStack = new FullStack
- {
- Name = "Rafnas",
- Experience = 4,
- WorkDescription = "C#, WPF, ASP.Net MVC, SQL Server, Xamarin, etc."
- };
-
- fullStack.NameSpec();
- fullStack.RoleSpec();
- fullStack.ExperianceSpec();
- }
Output
My name is Rafnas
My Skills are C#, WPF, ASP.Net MVC, SQL Server, Xamarin, etc.
My Experience is 4 years.