Learn About Extension Methods In C#

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.
  • 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,
  1. static class DoubleExtension {  
  2.         public static double Round(this double value, int digits) {  
  3.             return Math.Round(value, digits);  
  4.         }  
  5.     }  
After that we can use above extension method as shown below,
  1. class Program {  
  2.         static void Main(string[] args) {  
  3.             var a = 32.56829;  
  4.             var result = a.Round(2);  
  5.             Console.WriteLine(a);  
  6.             Console.WriteLine(result);  
  7.       }}  
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,
  1. var result = a.Round(2);  
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
  1. public interface IDeveloper {  
  2.         string Name { getset; }  
  3.         string WorkDescription { getset; }  
  4.         int Experience { getset; }  
  6.     }  
Next let’s create two types of developer classes implementing IDeveloper Interface as shown below
  1. public class FullStack : IDeveloper {  
  2.     public string Name { getset; }  
  3.     Public string WorkDescription { getset; }  
  4.     public int Experience { getset; }  
  5. }  
  7. public class Android : IDeveloper {  
  8.     public string Name { getset; }  
  9.     public int Experience { getset; }  
  10.     Public string WorkDescription { getset; }  
  11.     public void OtherFunction() { /*...*/ }  
  12. }  
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
  1. public static class IDeveloperExtension   
  2. {  
  4.     public static void NameSpec(this IDeveloper dev)   
  5.     {  
  6.         Console.WriteLine("My name is "+ dev.Name);  
  7.     }  
  9.     public static void RoleSpec(this IDeveloper dev)   
  10.     {  
  11.         Console.WriteLine("My Skills are "+ dev.WorkDescription);  
  12.     }  
  14.     public static void ExperianceSpec(this IDeveloper dev)   
  15.     {  
  16.         Console.WriteLine("My Experience is "+{dev.Experience}   
  17. +" years.");  
  18.     }  
  19. }  
After declaring the above extension methods for IDeveloper, we can write the below code to run
  1. static void Main(string[] args) {  
  2.     var fullStack = new FullStack   
  3.     {  
  4.         Name = "Rafnas",  
  5.         Experience = 4,  
  6.         WorkDescription = "C#, WPF, ASP.Net MVC, SQL Server, Xamarin, etc."  
  7.     };  
  9.     fullStack.NameSpec();  
  10.     fullStack.RoleSpec();  
  11.     fullStack.ExperianceSpec();  
  12. }  
My name is Rafnas
My Skills are C#, WPF, ASP.Net MVC, SQL Server, Xamarin, etc.
My Experience is 4 years.

Similar Articles