UrbanPro
true

Learn C# .NET from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Extension method In C#

Surya Prakash P.
03/06/2017 0 0

Introduction.

Hello Guys,

In this article, I will explain what is extension method and why and where we should use this.

Let’s understand the term extension means we are going to create something new with the help of old made things or objects etc. right? So this is very predictable word in real world. On other hand in programming world this meaning is almost same with some new enhancements.

Let’s see the professional definition in terms of oops.

Extension method is special kind of static method which we can access throughout or outside of program.

Extension method help us to extend the class with new methods without any changes in base type or class .

We can create new methods in the dll library class also without changing the old code though extension method.

Let’s see the simple example of extension method using System;

namespace csharpdemoexample

{

public static class csharpdemo

{

public static void Main()

{

try

{

Console.WriteLine(Myextmethod(“Welcome”, “Extension”));

Console.Read();

}

catch (Exception ex)

{

throw ex;

}

}

public static string Myextmethod(this string a, string b)

{

return (a + ” ” + b); // This is the extension method named Myextmethod.

}

}

}

All right. Now one question would come to your mind is, why I have written the this keyword in parameter right?

So let me clarify this confusion. This is the keyword which require for first argument parameter no matter what data type you use for this. But this keyword is mandatory for extension method. If you are not defining the this, it will acts like a normal static method .

Now next question is how to use the extension method in dll file. In other words, if you have created the dll in C# so all the classes and method will be act as private. So how can we use the same dll and create the new method inside your new application.

So let’s see how we can achieve this task? Let’s add a Class library in your solution project.addclasslib

Created two methods inside dll class. Add and subtract right .

BaseDLL

Now I want to use this DLL into some project and using this class, I want to create one new method.

Like Multiply. So we will achieve this by extension method. Let’s see how? First, add the DLL reference inside you project in which you want to implement the new method. Okay?

AddDLLRef

 

using BaseDLL;

namespace csharpdemoexample

{

public static class csharpdemo

{

public static int Multiply(this MathDemo obj ,int a, int b)

{

return a * b; //This is the extension method i have created using DLL.

}

public static void Main()

{

try

{

MathDemo obj2 = new MathDemo();

Console.WriteLine(“Result of extension method multiply is :” + Multiply(obj2,10,20));

Console.Read(); //I have called the newly created method here. And it works like charm. Great isn't it ?

}

catch (Exception ex)

{

throw ex;

}

}

}

}

resultextmethod

Conclusion – Now I am done. So in this article we have seen what is extension method and how to create and use them.

Thanks for reading this article.

I hope this article will be helpful for you guys, your comments and feedback are always welcome.

Thanks,

Surya Prakash Pandey.

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

ASP.net MVC 5
ASP.NET MVC It’s a part of ASP.NET and provides an alternative to ASP.NET webforms. You can even mix webforms and MVC. Basic concepts like state management are still the same. Thus, if you have...

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

.Net is not tough just it requir skill build
Hello friends which one is better java or .net ? Well you need to know both to make a right full argument. My advice would be pick any one get skilled on that and then learn second one. I believe by...

What Is An Array? Explain Its Type.
Array: Array is a collection of similar type of data in an array varaiable. Array start with Zero Index and maximum length of array is total element - 1. i.e., Suppose n is the length of array, total element...
N

ASP.NET - Introduction
ASP.NET is a web development platform, which provides a programming model, a comprehensive software infrastructure and various services required to build up robust web applications for PC, as well as mobile...
X

Looking for C# .NET Classes?

The best tutors for C# .NET Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C# .NET with the Best Tutors

The best Tutors for C# .NET Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more