处理需要字符串的枚举的好方法是什么

时间:2010-10-02 07:48:18

标签: c# enums

我很确定枚举不是我想要的。我想要的是一个命名项目列表

CustomerLookup = "005",
CustomerUpdate = "1010"

“005”和“1010”不是我的值,它们是我需要发送给我无法控制的第三方的值。其中有近500个。我只是希望我的代码看起来不错。

而不是

SendRequest("005");

我宁愿看

SendRequest(RequestType.CustomerLookup);

任何人都有任何自我记录的想法而不会在代码中疯狂吗?

3 个答案:

答案 0 :(得分:8)

有什么问题:

public static class RequestType
{
     public static readonly string CustomerLookup = "005";
     // etc
}

public static class RequestType
{
     public const string CustomerLookup = "005";
     // etc
}

?或者如果你想要更多类型安全:

public sealed class RequestType
{
     public static readonly RequestType CustomerLookup = new RequestType("005");
     // etc

     public string Code { get; private set; }

     private RequestType(string code)
     {
         this.Code = code;
     }
}

这基本上会给你一组固定的值(构造函数是私有的,所以外部代码不能创建不同的实例),你可以使用Code属性来获取相关的字符串值。

答案 1 :(得分:0)

如何使用某种associative array

答案 2 :(得分:0)

你已经这样做的方式似乎对我而言。

您明确地在代码中定义了请求类型值而没有含糊不清,当您使用它们时,您可以在智能感知方面使用智能感知。

我认为真正的问题是如何在没有任何tyypos的情况下将500个值放入代码中!

相关问题