c#:如何使用枚举来存储字符串常量? [重复]

时间:2022-06-04 00:15:35

Possible Duplicate:
Enum with strings

可能重复:带字符串的枚举

is is possible to have string constants in enum like

可以在枚举中使用字符串常量

      enum{name1="hmmm" name2="bdidwe"}

if it is not so what is best way to do so?

如果不是这样,最好的方法是什么?

I tried it its not working for string so right now i am grouping all related constnats in one class like

我试过它不能用于字符串所以现在我将所有相关的constnats分组在一个类中

      class operation
      {
          public const string  name1="hmmm";
          public const string  name2="bdidwe"
      }

4 个解决方案

#1


94  

Enum constants can only be of ordinal types (int by default), so you can't have string constants in enums.

枚举常量只能是序数类型(默认为int),因此枚举中不能包含字符串常量。

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; }
}

#2


36  

You could do this using DescriptionAttribute, but then you'd have to write code to get the string out of the attribute.

您可以使用DescriptionAttribute执行此操作,但之后您必须编写代码以从属性中获取字符串。

public enum YourEnum
{
    [Description("YourName1")]
    Name1,

    [Description("YourName2")]
    Name2
}

#3


5  

The whole point of enums is to be ordinal constants.
However, you can achieve what you want by using an extension method:

枚举的全部意义是序数常数。但是,您可以使用扩展方法实现所需目的:

  enum Operation
  {
      name1,
      name2
  }

  static class OperationTextExtender
  {
        public static String AsText(this Operation operation)
        {
              switch(operation)
              {
                    case Operation.name1: return "hmmm";
                    case Operation.name2: return "bdidwe";
                    ...
              }
        }
  }

  ...
  var test1 = Operation.name1;
  var test2 = test1.AsText();   

#4


0  

Your operation class won't compile as is... you didn't declare the type of name1 and name2...

你的操作类不会按原样编译...你没有声明name1和name2的类型......

But that is the approach I'd take... yes.

但那是我采取的方法......是的。

If you make it a struct then it becomes a value type which may or may not be what you wanted...

如果你把它变成一个结构,那么它就变成了一个值类型,可能是也可能不是你想要的......

#1


94  

Enum constants can only be of ordinal types (int by default), so you can't have string constants in enums.

枚举常量只能是序数类型(默认为int),因此枚举中不能包含字符串常量。

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; }
}

#2


36  

You could do this using DescriptionAttribute, but then you'd have to write code to get the string out of the attribute.

您可以使用DescriptionAttribute执行此操作,但之后您必须编写代码以从属性中获取字符串。

public enum YourEnum
{
    [Description("YourName1")]
    Name1,

    [Description("YourName2")]
    Name2
}

#3


5  

The whole point of enums is to be ordinal constants.
However, you can achieve what you want by using an extension method:

枚举的全部意义是序数常数。但是,您可以使用扩展方法实现所需目的:

  enum Operation
  {
      name1,
      name2
  }

  static class OperationTextExtender
  {
        public static String AsText(this Operation operation)
        {
              switch(operation)
              {
                    case Operation.name1: return "hmmm";
                    case Operation.name2: return "bdidwe";
                    ...
              }
        }
  }

  ...
  var test1 = Operation.name1;
  var test2 = test1.AsText();   

#4


0  

Your operation class won't compile as is... you didn't declare the type of name1 and name2...

你的操作类不会按原样编译...你没有声明name1和name2的类型......

But that is the approach I'd take... yes.

但那是我采取的方法......是的。

If you make it a struct then it becomes a value type which may or may not be what you wanted...

如果你把它变成一个结构,那么它就变成了一个值类型,可能是也可能不是你想要的......