我尝试使用或返回时可以避免使用枚举值吗?

如果我有以下枚举:

public enum ReturnValue{ Success = 0, FailReason1 = 1, FailReason2 = 2 //Etc... } 

我回来时可以避免施放,像这样:

 public static int main(string[] args){ return (int)ReturnValue.Success; } 

如果没有,为什么默认情况下不将枚举值视为int?

枚举应该是类型安全的。 我认为他们并没有让他们暗中倾倒以阻止其他用途。 虽然框架允许您为它们分配常量值,但您应该重新考虑您的意图。 如果您主要使用枚举来存储常量值,请考虑使用静态类:

 public static class ReturnValue { public const int Success = 0; public const int FailReason1 = 1; public const int FailReason2 = 2; //Etc... } 

这可以让你这样做。

 public static int main(string[] args){ return ReturnValue.Success; } 

编辑

当您想要为枚举提供值时,您希望将它们组合在一起。 见下面的例子:

 [Flags] // indicates bitwise operations occur on this enum public enum DaysOfWeek : byte // byte type to limit size { Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8, Thursday = 16, Friday = 32, Saturday = 64, Weekend = Sunday | Saturday, Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday } 

然后可以通过使用按位数学来消耗此枚举。 有些应用,请参阅以下示例。

 public static class DaysOfWeekEvaluator { public static bool IsWeekends(DaysOfWeek days) { return (days & DaysOfWeek.Weekend) == DaysOfWeek.Weekend; } public static bool IsAllWeekdays(DaysOfWeek days) { return (days & DaysOfWeek.Weekdays) == DaysOfWeek.Weekdays; } public static bool HasWeekdays(DaysOfWeek days) { return ((int) (days & DaysOfWeek.Weekdays)) > 0; } public static bool HasWeekendDays(DaysOfWeek days) { return ((int) (days & DaysOfWeek.Weekend)) > 0; } } 

没有隐式转换,因为枚举不必使用int作为基础类型。 例如,如果你的枚举使用uint作为底层类型,则没有从uint到int的隐式转换。

c#enum没用。

您可以避免从类型转换并通过创建密封类并提供隐式/显式转换运算符来约束可以显式转换为您的类型的值。

  • 提供一个隐式运算符,用于从您的类型转换为genericsint,因此您不必进行强制转换。
  • 提供一个显式运算符,用于从int转换为您的类型,如果整数无法满足约束,则抛出错误,例如(int x)=>(x> = 0 && x <= 2)。

如果使用此技术,请创建一个通用的不可变基类,例如ConstrainedNumber ,它具有一个接受T值的构造函数和约束delegate bool NumberConstraint(T value)delegate bool NumberConstraint(T value) 。 构造函数应该通过约束委托运行值,如果无法满足约束,则抛出exception。 基类还应该处理对T的隐式转换操作,并且应该通过重载object.Equals(object)和object.GetHashCode()来处理相等性,为ConstrainedNumber类型定义==和!=运算符,并且实现IEquatableIEquatable> 。 我还建议为基类和所有派生类型定义一个复制构造函数。 然后,通过reflection检索复制构造函数,可以在基类中干净地实现克隆,但这完全是可选的。 你可以自己弄清楚ConstrainedNumber实现,除非我已经在某个地方的stackoverflow上发布了它。

您可以在派生的ConstrainedNumber中提供命名的静态只读值,以便您可以像枚举一样访问它们。

 public sealed class ReturnValue: ConstrainedNumber { public static readonly NumberConstraint constraint = (int x) => (x >= 0 && x < 3); public static readonly ReturnValue Success = new ReturnValue(0); public static readonly ReturnValue FailReason1 = new ReturnValue(1); public static readonly ReturnValue FailReason2 = new ReturnValue(2); private ReturnValue( int value ): base( value, constraint ) {} private ReturnValue( ReturnValue original ): base (original) {} //may be used to support IClonable implementation in base class public static explicit operator ReturnValue( int value ) { switch(value) //switching to return an existing instance is more efficient than creating a new one and re-checking the constraint when there is a limited number of allowed values; if the constraint was more generic, such as an even number, then you would instead return a new instance here, and make your constructors public. { case 0: return Success; case 1: return FailReason1; case 2: return FailReason2; } throw new ArgumentException( "Value fails to meet the constraint defined for " + typeof(ReturnValue).FullName + ".", "value" ); } } 

您可以将此技术用于任何约束。 例如,名为EvenNumber的类可能有一个约束,如果给定的数字是偶数,则返回true。 在这种情况下,您只需将构造函数设置为public,并简化静态转换运算符以返回新的EvenNumber,而不是切换为返回有限的现有实例之一。

它可以像这样使用:

 EvenNumber x = (EvenNumber)2; EvenNumber y = (EvenNumber)3; //throws exception "Value fails to meet the constraint defined for {namespace}.EvenNumber." A c# enum would stupidly allow such a cast, creating an invalid EvenNumber, breaking the object-oriented model int z = x; //implicit conversion, no cast necessary; 

根据规范,枚举和整数根本不是可隐式转换的(除了文字0,允许进行比较测试/赋值/等)。 但是,只需要显式演员。

不,你不能避免铸造; 至于为什么没有隐含的转换,我不知道,但事实并非如此。

奇怪的是,这不是特定于.NET Framework,而是仅针对C#。 正如其他评论者已经指出的那样,在C#中,这基本上是语言的规范。 在VB.NET中也是如此。

在VB.NET中查看Enums的MSDN参考页面。 请注意,您可以在枚举声明时指定枚举的数据类型。

这意味着,如果你真的不想把你的代码乱丢到(int),你可以在VB.NET中编写枚举,将其声明为整数,然后使用C#中的Enum。

还记得他们告诉我们电脑会让我们的生活变得如此简单吗? 🙂

您可以将此行为归因于创建枚举背后的基本意图…创建一组命名常量,这些常量只能具有指​​定(或默认)值,具体取决于基础类型。

与您的问题相关,需要考虑两个单独的问题:

  1. 默认情况下, Enum值不能被视为int,因为这样您就可以提供任何整数,并且不会进行编译时检查以validation提供的整数实际上是否作为Enumeration中的值存在。

  2. 由于您尝试将控制类型(从System.Enum类派生的类型为YourCustomEnum )转换为基础类型(即intbyte等),因此需要进行转换。

如何使用类的静态成员?

 //enum DocInfos { DocName, DocNumber, DocVersion}; public class DocInfos { public static int DocName = 0; public static int DocNumer = 1; public static int DocVersion = 2; } 

  Doc = new string[DocInfos.DocVersion]; // Treffer Doc[DocInfos.DocName] = TrimB(HTMLLines[lineCounter + 2])