SIGN UP MEMBER LOGIN:    
ARTICLE

How to add methods to an enum in C#

Posted by Vulpes Articles | C# Language March 07, 2011
Given that C# was influenced by C and C++, this is no surprise as enums in those languages only contain constants as well. However, Java allows enums to include methods so how can we do something similar in C#?
Reader Level:

The problem

In C#, enums are simple value types which just contain a bunch of constants. No other type of member can be included.

Given that C# was influenced by C and C++, this is no surprise as enums in those languages only contain constants as well. However, Java allows enums to include methods so how can we do something similar in C#? 

The solution

The answer is to use extension methods which were introduced in C# 3.0.

Here's an example where we (effectively) add some methods to a Colors enum.

using System;
public enum Colors
{
    White,
    Red,
    Blue,
    Green
}
// extension methods for the Colors enum
public static class ColorsExtensions
{
    public static bool IsWhite(this Colors c)
    {
        return c == Colors.White;
    }
    public static bool IsRedOrBlue(this Colors c)
    {
        return c == Colors.Red || c == Colors.Blue;
    }
}
class Test
{
    static void Main()
    {
        Colors c = Colors.Red;
        Console.WriteLine(c.IsWhite()); // False
        Console.WriteLine(c.IsRedOrBlue());  // True
        Console.ReadKey();
    }
}

Is Microsoft likely to add methods to enums in future?

Although this feature is often requested, I would be surprised if they do given that this easy workaround exists.

However, if 'extension properties' were added to the language (a much more likely event), then we could use the same technique to add properties to enums as well.

Login to add your contents and source code to this article
share this article :
post comment
 

Good

Posted by Pradip Pandey Oct 12, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor