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

Export To Excel using StringBuilder in ASP.net MVC C#
public ActionResult Export() { StringBuilder sb = new StringBuilder(); //static file name, can be changes as per requirement string sFileName = "filename.xls"; //Bind...
M

Mohammad Shafi

1 0
1

C# - COLLECTIONS
Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.Collection...

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

What are Various Types of Exception in C#
ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException. NullReferenceException, IndexOutOfRangeException, SQL exceptions Stack Overflow Exception, Invalid Castexception ArgumentException...

Why Need for Delegates in C#
.1. TYPE SAFETY FEATURES. 2. OBJECT ORIENRED FEATURES. 3. Lamda Function 4. BRING GENERICS. 1. Type safety means an object when defined has its ability to understand of its own type and maintain...
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