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

What Is The Difference In For...Loop And Foreach Loop?
Loop is very important in C# Programing language. Loop is call iteration statement, loops are used for executing the repeated task. 1. For...Loop: The for…loop executes a statement or a block...
N

A very brief introduction to .NET framework
.NET development has started in the year 2000 , a product of Microsoft. From the begining of .NET development till now, it has got many RAD facilities. Why .NET? developed for distributed computing...

Trends in .Net Platform
.NET Framework is a software framework developed by Microsoft. It includes a large class library named Framework Class Library (FCL) and provides language interoperability (each language can use code written...

ASP .Net Interview Questions.
1. What is ASP? Active Server Pages (ASP), also known as Classic ASP, is a Microsoft's server-side technology, which helps in creating dynamic and user-friendly Web pages. It uses different scripting...

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