Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods inside enum in C#

People also ask

Can you add methods to enum?

You can use extension methods to add functionality specific to a particular enum type.

Can enum contain methods C#?

C# Does not allow use of methods in enumerators as it is not a class based principle, but rather an 2 dimensional array with a string and value.

Can we declare enum inside method?

We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.

Can enums have public methods?

Enum Fields When the constant enum values are defined, an int value is passed to the enum constructor. The enum constructor must be private . You cannot use public or protected constructors for a Java enum . If you do not specify an access modifier the enum constructor it will be implicitly private .


You can write extension methods for enum types:

enum Stuff
{
    Thing1,
    Thing2
}

static class StuffMethods
{

    public static String GetString(this Stuff s1)
    {
        switch (s1)
        {
            case Stuff.Thing1:
                return "Yeah!";
            case Stuff.Thing2:
                return "Okay!";
            default:
                return "What?!";
        }
    }
}

class Program
{


    static void Main(string[] args)
    {
        Stuff thing = Stuff.Thing1;
        String str = thing.GetString();
    }
}

You can write an extension method for your enum:

How to: Create a New Method for an Enumeration (C# Programming Guide)


Another option is to use the Enumeration Class created by Jimmy Bogard.

Basically, you must create a class that inherits from his Enumeration. Example:

public class EmployeeType : Enumeration
{
    public static readonly EmployeeType Manager 
        = new EmployeeType(0, "Manager");
    public static readonly EmployeeType Servant 
        = new EmployeeType(1, "Servant");
    public static readonly EmployeeType Assistant
        = new EmployeeType(2, "Assistant to the Regional Manager");

    private EmployeeType() { }
    private EmployeeType(int value, string displayName) : base(value, displayName) { }

    // Your method...
    public override string ToString()
    {
        return $"{value} - {displayName}!";
    }
}

Then you can use it like an enum, with the possibility to put methods inside it (among another things):

EmployeeType.Manager.ToString();
//0 - Manager
EmployeeType.Servant.ToString();
//1 - Servant
EmployeeType.Assistant.ToString();
//2 - Assistant to the Regional Manager

You can download it with NuGet.

Although this implementation is not native in the language, the syntax (construction and usage) is pretty close to languages that implement enums natively better than C# (Kotlin for example).


Nope. You can create a class, then add a bunch of properties to the class to somewhat emulate an enum, but thats not really the same thing.

class MyClass
{
    public string MyString1 { get{ return "one";} }
    public string MyString2 { get{ return "two";} }
    public string MyString3 { get{ return "three";} }

    public void MyMethod()
    {
        // do something.
    }
}

A better pattern would be to put your methods in a class separate from your emum.


Since I came across, and needed the exact opposite of enum to string, here is a Generic solution:

static class EnumExtensions {
    public static T GetEnum<T>(this string itemName) {
        return (T) Enum.Parse(typeof(T), itemName, true);
    }
}

This also ignores case and is very handy for parsing REST-Response to your enum to obtain more type safety. Hopefully it helps someone