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

Pro Developer Program Syllabus
What you will learn in Pro-Developer Program: i. Introduction to Web Technologies. ii. Introduction to .Net - Features of .Net, CTS, CLS, CLR and MSIL. iii. C# & .Net Basics - Data Types,...

Difference between Abstract Class and Interface
This is probably one of the most commonly asked questions in any job interview for .Net. Before we look into the differences between the two lets find out the main features. Abstract Class By definition...

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

Multi-core CPU utilization using Task Parallel Library in c#
Nowadays, we are using multiple core systems. We must write our .NET applications in such a way that we must utilise the complete computing power of the machine. The parallel task library(TPL) allows...

What are various validator in ASP.NET 2.0 and features common in all validation control in ASP.NET
Control to validate an error message. Are common feature in all the controls.Various validators are requiredfeild validator ,compare validator ,regular expression validator,custom validator,range validator...
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