Extension methods in .NET are a great way to add functionality to the existing type without actually having to modify or redefine it for the purpose. These are static methods and can be called on instance of a class just as a normal instance method.
Extension method is just an extension of pre-existing type. It is a new feature introduced in .NET framework as the type extension. Extension methods are generally the resemblance of static methods that are actually implemented as instance methods on the extended type. One using .NET framework of 3.5 or above version can implement these extension methods in order to add functionality to the existing type without actually having to modify or redefine it for the purpose. In order to add extension methods to a class you do not require changing it’s source code. With these methods one can easily customize the definition of a class by adding custom functions to that class. Just any other method of a class, extension method can also be called on any instance of the class.
How are Extension Methods created in .NET?
Creating an extension method in .net is as simple as creating a class, defining a function and making the function as static. Let us now consider an example to define the process of creating these methods of easy code extension in .NET.
//Extension Method Sample Program using C# in .NET
For example, now we are going to create a class by name TrainingInstitute and define a simple function in it with the name TrainingCourse() that returns String value. Now, suppose if you want to extend this class to add another method say TrainingCourse1().There are two ways to do this, either you need to extend this class to another type TrainingInstitute2 derived from TrainingInstitute and include that method into it, or use extension method to add it directly within the type TrainingInstitute itself. Then following is the process to do.
First of all, you are required is to create a class.
public class TrainingInstitute
public string TrainingCourse(String str)
Create TrainingCourse1, an extension method which is included with TrainingInstitute. Never forget that an extension method should be static. Moreover the very first argument of this method is of type in which the method is to be included with a “this” keyword in front of it. This represents the extension method.
public static class Extensions
public static string TrainingCourse1(this TrainingInstitute obj)
//This is extension method of TrainingInstitute class
How to Call Extension Method?
Calling an extension method is just as simple as calling any normal class method with an instance. Just have a look at following method where the normal and instance methods are called using the object of the same class “TrainingInstitute”.
TrainingInstitute objti = new TrainingInstitute();
Console.WriteLine(objti. TrainingCourse());//Value from Instance method
Console.WriteLine(objti. TrainingCourse1());//Value from Extension method
Hence, this way one can create and use extension methods in a .NET program using C#. To learn more about extension methods in .Net in a better way, one can take up .NET training at a reputed institute. The experts at a .NET training institute can teach you in detail about extension methods that too with practical examples which can give you better understanding of the concept than self understanding by reading from the internet.
TOPS Technologies is a top .NET training institute in Ahmedabad, Gujarat offering industry-oriented training for the freshers looking for .NET jobs. The live project training and Microsoft certification training is offered by the institute under the highly experienced and competent trainers.