Extension methods

            1. What do extension methods mean?

            2. How to declare extension methods?

            3. What are the rules to resolve a method call?

            Extension methods are a way to expand functionality of a type by adding new methods without deriving it into the type.

            To declare extension methods we must create a static type and mark an extension method as public and static. The first parameter of an extension method must have the type of the extensible type and marked by this keyword.   

Example 1:

    public class MyType

    {

        public string Data = "My Type data";

 

        public void Show()

        {

            Trace.WriteLine(Data);

        }

    }

 

    public static class Visualizer

    {

        public static void Show(this object type)

        {

            Trace.WriteLine("Object");

        }

 

        public static void Show(this MyType type)

        {

            Trace.WriteLine("MyType");

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            MyType myType = new MyType();

            myType.Show();

            Console.ReadKey();

        }

    }

The order of calling Show() method:

  1. At first the instance method will be called.
  2. The method which has a concrete type as an extension method paramter.
  3. The method which has a base type as an extension method paramter.

So the priority of instance methods is higher than extension methods.

Example 2:

    public class A

    {

        public virtual void X()

        {

            Trace.WriteLine("MethodX of A class");

        }

    }

 

    public class B : A

    {

        public override void X()

        {

            Trace.WriteLine("MethodX of B class");

        }

 

        public void Y()

        {

            Trace.WriteLine("MethodY of B class");

        }

    }

 

    public static class E

    {

        public static void X(this A value)

        {

            Trace.WriteLine("MethodX of E class");

        }

 

        public static void Y(this A value)

        {

            Trace.WriteLine("MethodY of E class");

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Trace.WriteLine("-=A a = new A();=-");

            A a = new A();

            a.X();

            a.Y();

 

            Trace.WriteLine("-=B b = new B();=-");

            B b = new B();

            b.X();

            b.Y();

 

            Trace.WriteLine("-=A c = new B();=-");

            A c = new B();

            c.X();

            c.Y();

 

            Console.ReadKey();

        }

    }

Output:

-=A a = new A();=-

MethodX of A class

MethodY of E class

 

-=B b = new B();=-

MethodX of B class

MethodY of B class

 

-=A c = new B();=-

MethodX of B class

MethodY of E class

 

Note that in case of calling c.Y() the E.Y() method is called because the c variable is defined as an A type and the A type doesn’t have its Y() method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s