对不起,如果问题的标题令人困惑,但我不知道该怎么问.真正想要的是拥有永不改变的只读数据.
目前我有两个枚举MeterType
和SubMeterType
public enum MeterType
{
Water = 1001,
Electricity = 1004,
Gas = 1007
}
Run Code Online (Sandbox Code Playgroud)
和
public enum SubMeterType
{
DrinkingWater = 1002,
UsageWater = 1003,
SubsidiseGas = 1008,
NonSusbsidisedGas = 1009
}
Run Code Online (Sandbox Code Playgroud)
现在我想用这些如下
获取MeterType
string meterType = MeterType.Water.ToString("d");
Run Code Online (Sandbox Code Playgroud)
并获得SubMeterType,是否可能有类似的东西
string subMeterType = MeterType.Water.DrinkingWater("d");
Run Code Online (Sandbox Code Playgroud)
应该使用具有readonly属性的类的另一种方法吗?或修改这些枚举以满足我的要求.
您可以在嵌套类中使用常量整数,而不是使用枚举:
public static class MeterType
{
public const int Water = 1001;
public const int Electricity = 1004;
public const int Gas = 1007;
public static class Waters
{
public const int DrinkingWater = 1002;
public const int UsageWater = 1003;
}
public static class Gases
{
public const int SubsidiseGas = 1008;
public const int NonSusbsidisedGas = 1009;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4065 次 |
最近记录: |