Tuesday, September 6, 2011

How to extend Interface using Extension Methods in C#


Extension methods are used to extend an existing type with new methods without changing original type. Extension methods were introduced in C# 3.0. Extension methods are defined as static method inside static class and the first parameter must be this. In this article I will explain how to extend interface using extension method?

public static class MyExtensionMethods
{
    public static void MyMethod(this ITestInterface Test)
    {
        Console.WriteLine("Extension Method...");
    }
}
public interface ITestInterface
{
    void Display(string name);
}
public class TestClass : ITestInterface
{
    public void Display(string name)
    {
        Console.WriteLine("Hello" + name);
    }
}
public static void Main()
{
    ITestInterface test = new TestClass();
    test.Display("Mitesh");  //Output - Hello, Mitesh
    test.MyMethod();    // Output - Extension Method...
}

As shown in above code, ITestInterface is extended with new method called MyMethod. To extend interface one static method MyMethod is created inside MyExtensionMethods static class. MyMethod has ‘this’ as first parameter and followed by the interface type (which we need to extend) in our case ITestInterface. We can also access properties of ITestInterface inside MyMethod using Test instance of ITestIterface.


See also
How to suppress compiler generated warnings in Visual Studio
Delegates Vs. Interface
Partial Methods in C#

7 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training institute in velachery | dot net course fees

    ReplyDelete
  2. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | dot net training in chennai

    ReplyDelete
  3. The blog gave me idea to extend interface using extension methods in C# my sincere thanks for sharing this post
    Dot Net Training in Chennai

    ReplyDelete
  4. you have been shared really informative blog. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    dot net training in chennai

    ReplyDelete
  5. Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

    Installment loans
    Payday loans
    Title loans
    Cash Advances

    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Online Training from India. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    JavaScript Online Training from India

    ReplyDelete