Enum constants can only be of ordinal types (int
by default), so you can’t have string constants in enums.
When I want something like a “string-based enum” I create a class to hold the constants like you did, except I make it a static class to prevent both unwanted instantiation and unwanted subclassing.
But if you don’t want to use string as the type in method signatures and you prefer a safer, more restrictive type (like Operation
), you can use the safe enum pattern:
public sealed class Operation
{
public static readonly Operation Name1 = new Operation("Name1");
public static readonly Operation Name2 = new Operation("Name2");
private Operation(string value)
{
Value = value;
}
public string Value { get; private set; }
}