Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Extension Methods in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 1.88k
    Comment on it

    Extension Methods in C#

     

    Extension Methods were introduced in c# 3.0. These methods are helpful in adding methods to the types which already existed i.e without creating a derived type via inheritance, methods can be added to existing type. In short functionality of existing type is extended.

     

    Creation and Usage of Extension method   

     

    Extension method can be defined as static method within static class where first parameter in this method is used with "this" modifier and type of this parameter is of the type that is extended.

     

    Program demonstrating usage of extention methods

     

    First create a blank solution named "ExtmethodExample" in visual studio 2015. Create a project Class Library named as "Extmethod".

     

    Now create another project console application named "ExtendingExtmethod" in  "ExtmethodExample" solution and include the reference of class library created above in this project.

     

    Including class library "Extmethod" reference in "ExtendingExtmethod".

     

     

     

    Solution folder looks like the following :-

     

     

    Code within a class of Class library named "Extmethod"

     

    namespace Extmethod
    {
        public class Class1
        {
            public string Display()
            {
                return ("Within Display Method");
            }
    
            public string Print()
            {
                return ("Within Print Method");
            }
        }
    }

     

    Code within "ExtendingExtmethod" :

     

    using System;
    using Extmethod;
    
    namespace ExtendingExtmethod
    {
        // without creating a derived type via inheritance, methods can be added to existing type through extension method
        public static class Demo                       
        {
            public static void Test(this Class1 ob)   
            {
                Console.WriteLine("This is an extension method");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Class1 obj = new Class1();
                Console.WriteLine(obj.Display());
                Console.WriteLine(obj.Print());
                obj.Test();       //extension method are called as if they are instance methods on the extended type
                Console.ReadKey();
            }
        }
    }

     

    Output when above application runs :

     

     

    Advantage of using Extension Methods :-

     

    • Existing class can be extended via extension method as a result for extension existing class need not to rely on inheritance or class source code modification is not required.

     

    • For sealed class functionality can only be extended via extension method as sealed classes are classes which cannot be inherited.

     

    • For using dynamism of C# enhancements in class's design,this feature is important.

     


    Points to Remember :

     

    • Definition of extension method should be in top-level static class.

     

    • An extension method having same declaration as an instance method will not be called.

     

    • Existing methods cannot be overridden via extension methods.

     

    • Fields, properties or events does not make use of the concept of extension method.

     

    • The extension methods should not be overused as it does not show good style of programming.

     

    Extension Methods in C#

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: