C# · Dotnet

Effective Extension methods with Enums

Dear Reader,

Today i learn something interesting and quite effective with the C# feature extension methods along with enums. There are at times we need to do some extensible operations on enums. Unlike structs/classes, enums do not provide us any space in itself to add Methods for doing some operations on it or its data members.

Yes there is always a crude way of doing this without using of extension methods at all. But think about maintainability of your code. What if this code chunk (operation) needs to be reused at multiple places in your code. Yes we could push the code chunk to a method and then to a reusable class. Though this is half way approach, extension methods do provide us a near approach in fact.

This is why it’s quite interesting to me and i always admire and look for places to use this feature only if it is justifiable. So below is one similar chunk of code which i came up with (not the real exact code) but i  do believe it does represents the intent meaning.

 public enum WorkingDays
    {
        Monday,
        Tuesday,
        Wednesday,
        Thurday
    }

    public static class DaysOfWeekEnumExtension
    {
        public static bool IsWorkingDay(this WorkingDays workingDay, string currentDate)
        {
            bool result = false;
            if(string.IsNullOrWhiteSpace(currentDate))
                result = false;
            else result = Enum.IsDefined(typeof(WorkingDays),currentDate);

            return result;
        }
    }    

   public partial class Program
    {
         public static void Main()
        {
            WorkingDays workDays = default(WorkingDays);//Assume this value comes from a remote service
            DateTime dt = DateTime.Now;
            dt = dt.AddYears(1).AddMonths(5);
            Console.WriteLine("Some day : " + dt.DayOfWeek.ToString() + " to be checked as working day " +
                " =>" + workDays.IsWorkingDay(dt.DayOfWeek.ToString()));
            Console.ReadKey();
        }
    }

As you can see, in the main method, the interface for using this extension along with the enum i have defined became very simpler and cleaner. The reason i have used default() keyword instead of directly assigning one of the member of Enum because i assumed that i may not know what should be the default value. It could be any thing from the enum members. So i have left the default() to choose. In real world, assume i may get the values from a remote service or a web service perhaps.

I am sure there are many more great examples than above. I would be happy to learn them. So please do a comment.

Thanks and Happy Coding,
Zenwalker 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s