Friday, July 18, 2014

Extension Methods in c#


If one wants to extend a property of a class without modifying the existing code of class and compile it again one can use Extension methods concept which has been introduced in .Net 3.5 and C#3.0.

Previously if one wants to extend any particular class one had to inherit that class and create a new class and then give its implementation but using extension methods one can easily extend functionality of an existing class.

To explain the concept better I will take example. Lets say I have a following class BookMyShow which provided different methods to provide booking of different shows. On top of that this class is sealed which means this class cant be inherited further which means in future if I want to extend the functionality of this class I need to have either access to the source code of this class and then change the source code and compile it and then re-distribute the same..........ufffffffffffffffff.... lot of things to do.

namespace BookingPlaza
{
    class BookMyShow
    {
        public bool Book(string[] strppl)
        {
            /// ;;.......
            return true;
        }

        public int CancelBooking(string[] strArrBook)
        {
            return 0;
        }

        public DateTime GetShowBooking(string strWhen)
        {
            DateTime dtm = new DateTime();
            switch (strWhen)
            {
                case "AM":
                    {
                        DateTime.TryParse("10:00 AM",out  dtm);
                        break;
                    }
                case "PM":
                    {
                        DateTime.TryParse("10:00 PM", out dtm);
                        break;
                    }
                default:
                    DateTime.TryParse("03:00 PM", out dtm);
                    break;

            }

            return dtm;
        }
    }
}


Here comes the extension method to rescue.

Using extension method one can extend the functionality without source code access of the type to be extended

namespace ExtensionOfBMS
{
    static class ExtendBookMyShow
    {
        public static bool AdvanceBooking(this BookMyShow objBMS, DateTime timings)
        {
            return true;
        }

    }
}

As you can see above we extended the BookMYShow class but you might be wondering how this happened as I was so whole magic lies in the first parameter of the static method.

So first I will explain some syntax:

1. You need to create a static class having static method which will have extended functionality of the type to be extended.
2. Here comes the magic ;) . First parameter of the function should be type to be extended with this modified specified. As in this case it was BookMyShow.
3. Now where ever you want to use it you just need to put using directive. Like using ExtensionOfBMS.; and then you can access the extended method just like a instance method. As you can see in the screen shot AdvanceBooking is available as the extension method.




I would say amazing thing in this extension method implementation is that a static method is just available as instance method of the class when IL code is generated its actually static class call to static methods.

One more thing to point here is binding. If you create a same method as extension method which is already defined in the class and when you call this extension method it will always going to call the type implementation instead of the extended one. So for example I try to create one more extension method having same signature as already existing method compiler will always pick base class implementation.

Extension methods cant access the private methods or variables of the type or class which they are extending hence encapsulation is still intact in the design.

Keep extending!!!!!!!!!!!!!!!!!!!!!!!

No comments:

Post a Comment