选项1
使用enum Measurement
andDictionary<Measurement, string>
映射 fromMeasurement
到显示名称。
public enum Measurement
{
Grams,
FluidOunces,
...
}
private static Dictionary<Measurement, string> displayName = new Dictionary<Measurement, string>
{
{ Measurement.Grams, "Grams" },
{ Measurement.FluidOunces, "Fluid Ounces" },
...
};
public static string DisplayName(Measurement measurement)
{
return displayName[measurement];
}
选项 2
class Measurement
没有公共构造函数、有public static readonly
实例并且有属性的使用。
public class Measurement
{
public string DisplayName { get; private set; }
private Measurement(string displayName)
{
this.DisplayName = displayName;
}
public static readonly Measurement Grams = new Measurement("Grams");
public static readonly Measurement FluidOunces = new Measurement("Fluid Ounces");
...
}