ITORIAN    

  public string Welcome() {

      return "Abhimanyu's Thoughts";

  }

My First E-Book
Quick Report
Yearly Posts:
   i) Oct-Nov 2010: 12
   ii) In 2011: 380
   iii) In 2012: 32

You can find my articles and blogs on:
   i) itorian.com
   ii) c-sharpcorner.com
   iii) dotnetfunda.com
   iv) codeproject.com

Greatest Hits
People I Follow
Disclaimer
This is my personal website and the opinions I have expressed here is my own. For any accuracy I recommend to visit official websites like MSDN for Microsoft. I developed this website to share my technical skills.

Extension Methods in C-Sharp
[In this post you will learn how to extend the methods (extension method) and a nice YouTube video clip explaining the use of Extension Method in C#.]
Abhimanyu Kumar Vatsa

IT Faculty || I blog on Microsoft Technologies || Mindcracker MVP || Founder of ITORIAN.COM
   

Introduction

Extension methods are a convenient way to add methods to classes that you don’t own and so can’t modify directly.

Look at the program given below:

using System;

using System.Collections.Generic;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the object of the class

            Person p = new Person() { Name = "Abhimanyu", Age = 22 };

 

            //see the result

            Console.WriteLine(p.Name + ", " + p.Age);

            Console.ReadKey();

        }

    }

 

    //class

    class Person

    {

        public string Name { get; set; }

        public int Age { get; set; }

    }

}

 

In the above program, I have a ‘Person’ class having two properties ‘Name’ and ‘Age’ and the same I have created an object of the ‘Person’ class in main method by the name ‘p’ and then assigning the property value.

Assume I want to extend the ‘Person’ class (reason may be anything like don’t own class etc.) then what we need to do is to extend the ‘Person’ class by using a static directive (class or method). Look at the program snap below.

//extension method

static class ExtensionMethod

{

    public static string Introduce(this Person person)

    {

        return string.Format("Hello {0} ", person.Name);

    }

}

 

In above code the key points are, class or method should static type and in ‘Introduce’ method parameters we need to write the name of class (as ‘Person’) which we are extending and its new name (as ‘person’).

Find the complete program here.

using System;

using System.Collections.Generic;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the object of the class

            Person p = new Person() { Name = "Abhimanyu", Age = 22 };

 

            //see the result

            Console.WriteLine(p.Introduce());

            Console.ReadKey();

        }

    }

 

    //class

    class Person

    {

        public string Name { get; set; }

        public int Age { get; set; }

 

        /*

        public string Speak()

        {

            return string.Format("Hello {0} ", Name);

        }

        */

    }

 

    //extension method

    static class ExtensionMethod

    {

        public static string Introduce(this Person person)

        {

            return string.Format("Hello {0} ", person.Name);

        }

    }

}

 

I hope you will enjoy learning it. Thanks


Happy Coding !!



Join us on Facebook. Join us on Twitter.

   



Approved Comments

Roshan 1/21/2012 4:29:20 PM
Roshan: Excellent Articles. Thanks a lot.
Author: You Welcome