#813 – Defining an Extension Method for an Enumerated Type

In addition to other types, you can extend the functionality of an enumerated type using extension methods.

Below is an extension method that extends the DayOfWeek enumerated type.

        public static string Activity(this DayOfWeek day)
            string activity = "";

            switch (day)
                case DayOfWeek.Sunday:
                    activity = "Reading paper";
                case DayOfWeek.Monday:
                    activity = "Grumbling";
                case DayOfWeek.Tuesday:
                    activity = "Eating tacos";
                case DayOfWeek.Wednesday:
                    activity = "Reading";
                case DayOfWeek.Thursday:
                    activity = "Cursing";
                case DayOfWeek.Friday:
                    activity = "Celebrating";
                case DayOfWeek.Saturday:
                    activity = "Hiking";

            return activity;

We can now call the Activity method on any variable or constant whose type is DayOfWeek.

            DayOfWeek today = DayOfWeek.Thursday;



About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #813 – Defining an Extension Method for an Enumerated Type

  1. Alian says:

    Very interesting sample. This blog is one of my favorites.

  2. Mourad Barakat says:

    Nice explanation. Thanks you Sean!

Leave a Reply to Mourad Barakat Cancel 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

%d bloggers like this: