Associating enums with strings in C#

2019-01-02 16:54发布

I know the following is not possible because it has to be an int

enum GroupTypes
{
    TheGroup = "OEM",
    TheOtherGroup = "CMB"
}

From my database I get a field with incomprehensive codes (the OEM and CMB's). I would want to make this field into an enum or something else understandable. Because the target is readability the solution should be terse.
What other options do I have?

标签: c# .net
30条回答
残风、尘缘若梦
2楼-- · 2019-01-02 17:07

Try adding constants to a static class. You do not end up with a Type, but you have readable, organised constants:

public static class GroupTypes
{
    public const string TheGroup = "OEM";
    public const string TheOtherGroup = "CMB"
}
查看更多
梦醉为红颜
3楼-- · 2019-01-02 17:09

C# doesn't support enumerated strings, but for most situations you can use a List or Dictionary to get the desired effect.

E.g. To print pass/fail results:

List<string> PassFail = new List<string> { "FAIL", "PASS" };
bool result = true;
Console.WriteLine("Test1: " + PassFail[result.GetHashCode()]);
查看更多
长期被迫恋爱
4楼-- · 2019-01-02 17:10

Another way to deal with the problem, is to have a enum and a array of strings that will map the enum values with the list of strings:

public enum GroupTypes
{
    TheGroup  = 0,
    TheOtherGroup 
}

string[] GroupTypesStr = {
    "OEM",
    "CMB"
};

you may use it something like this:

Log.Write(GroupTypesStr[(int)GroupTypes.TheOtherGroup]);

It will prompt CMB

PROS:

  1. Easy and clean code.
  2. High Performance (specially in comparison with those approaches that uses classes)

CONS:

  1. Prone to mess up the list when editing it, but it will be okay for a short list.
查看更多
爱死公子算了
5楼-- · 2019-01-02 17:11

I like to use properties in a class instead of methods, since they look more enum-like.

Here's a example for a Logger:

public class LogCategory
{
 private LogCategory(string value) { Value = value; }

 public string Value { get; set; }

 public static LogCategory Trace { get { return new LogCategory("Trace"); } }
 public static LogCategory Debug { get { return new LogCategory("Debug"); } }
 public static LogCategory Info { get { return new LogCategory("Info"); } }
 public static LogCategory Warning { get { return new LogCategory("Warning"); } }
 public static LogCategory Error { get { return new LogCategory("Error"); } }
}

Pass in type-safe string values as a parameter:

public static void Write(string message, LogCategory logCategory)
{
   var log = new LogEntry { Message = message };
   Logger.Write(log, logCategory.Value);
}

Usage:

Logger.Write("This is almost like an enum.", LogCategory.Info);
查看更多
回忆,回不去的记忆
6楼-- · 2019-01-02 17:12

In VS 2015, you can use nameof

public class LogCategory
{
    public static string Trace;
    public static string Debug;
    public static string Info;
    public static string Warning;
    public static string Error;
}

Usage:

Logger.Write("This is almost like an enum.", nameof(LogCategory.Info));
查看更多
与风俱净
7楼-- · 2019-01-02 17:16

If you're trying to make your code readable:

class GroupTypes {
    public static final String (whatever oem stands for) = "OEM";
    public static final String (whatever cmb stands for) = "CMB";
    ...
}

and if you need a list of them, include these finals in a static final List<String>. This example is in Java.

If you're trying to make your application readable, add:

public static final Map<String, String> groupsByDbValue;
static {
    groupsByDbValue = new HashMap<String, String>();
    groupsByDbValue.put("OEM", "(whatever OEM stands for)");
    groupsByDbValue.put("CMB", "(whatever CMB stands for)");
}
查看更多
登录 后发表回答