Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Extension Methods in C-Sharp

 by Abhimanyu Kumar Vatsa on Nov 23, 2011

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#.
Comments: 0 Views: 385 Printable Version 

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

Refer this YouTube video if you wish. Video
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Nevron Gauge for SharePoint
Become a Sponsor

 Blogger's Profile
Age: 24
Location:
Title: Developer
Joined: May 31, 2010
Education: Bachelors Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.