Asp.Net-training

.NET Training - Do You Know what are Extension Methods in .NET?

Author Image Icon

Hiren

Last Updated: 17 Nov 2023


Content Media

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 instances 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, the extension method can also be called on any instance of the class. You can master the skills of ASP.Net by enrolling into a ASP.Net Developer Course.
Placement Banner

Get 100% Job Assistance & get placed in your dream company

Job Assistance
3000+ Companies Tie-Ups

Enter Your Details Now
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.

Content Media

//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.

Step 1:

First of all, you are required is to create a class.

public class TrainingInstitute

{

public string TrainingCourse(String str)

{

return str;

}

}

Step 2:

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

{

return str;

}

}

Content Media

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

Console.Read();

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 ASP. Net Training in Ahmedabad, Gujarat offering industry-oriented training for freshers looking for .NET jobs. The live project training and Microsoft certification training is offered by the institute under highly experienced and competent trainers.

Author Bio: 

Niral Modi works as a Chief Executive Officer at TOPS Technologies, which is an Education company with an estimated 303 employees; and was founded in 2008. They are part of the Executive team within the C-Suite Department, and their management level is C-Level. Niral is currently based in Chicago, United States.

TOPS Technologies offer The Best ASP.Net Classes in Vadodara, Hardware Training Classes, Graphic Designing & Web Design Training through Live Project Training. Having tie-ups with 3000+ IT software development companies, We provide a 100% Job Guarantee in Software Development Courses. We are known for our training courses in PHP, Python, Java, Android & iOS, Asp.Net, C & C++, Angular Courses, IoT, Software Testing, CCNA, Cloud Computing, Ethical Hacking, Hardware Networking, Cyber Security Training, Digital Marketing and SEO. We also teach Laravel, Nodejs, Unity 3D Game Development, Machine Learning with Python, Data Science, and Linux server training. Check out our YouTube channel for more information on any course, such as ASP.Net Course, graphic design, and web design, among others.


Stay Connected