简体   繁体   English

C# 枚举类型安全吗?

[英]Are C# enums typesafe?

Are C# enums typesafe? C# 枚举类型安全吗?

If not what are the implications?如果不是,有什么影响?

To give a slightly different answer... while the values are type-safe from the casting perspective, they are still unchecked once they have been cast - ie 为了给出稍微不同的答案......虽然从投射角度看这些值是类型安全的,但是一旦它们被投射,它们仍然是未经检查的 - 即

enum Foo { A = 1, B = 2, C = 3 }    
static void Main()
{
    Foo foo = (Foo)500; // works fine
    Console.WriteLine(foo); // also fine - shows 500
}

For this reason, you should take care to check the values - for example with a default in a switch that throws an exception. 因此,您应该注意检查值 - 例如,在引发异常的switch中使用default

You can also check the (for non- [Flags] values) via: 您还可以通过以下方式检查(对于非 [Flags]值):

 
 
 
  
  bool isValid = Enum.IsDefined(typeof(Foo), foo);
 
  

Yes they are. 对,他们是。

The following is from http://www.csharp-station.com/Tutorials/Lesson17.aspx 以下内容来自http://www.csharp-station.com/Tutorials/Lesson17.aspx

Enums are strongly typed constants. 枚举是强类型常量。 They are essentially unique types that allow you to assign symbolic names to integral values. 它们本质上是唯一的类型,允许您将符号名称分配给整数值。 In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. 在C#传统中,它们是强类型的,这意味着即使其成员的基础值相同,也不能将一种类型的枚举隐式赋值给另一种类型的枚举。 Along the same lines, integral types and enums are not implicitly interchangable. 同样,整体类型和枚举也不是隐含地可互换的。 All assignments between different enum types and integral types require an explicit cast. 不同枚举类型和整数类型之间的所有赋值都需要显式强制转换。

For those suggesting to use Enum.IsDefined to do argument validation...don't! 对于那些建议使用Enum.IsDefined进行参数验证的人......不要! Per Brad Abrams (from the Framework Design Guidlines Update on Enum Design ): Per Brad Abrams(来自Enum Design框架设计指南更新 ):

There are really two problems with Enum.IsDefined(). Enum.IsDefined()确实存在两个问题。 First it loads reflection and a bunch of cold type metadata making it a deceptively expensive call. 首先,它加载反射和一堆冷类型元数据,使其成为一个看似昂贵的电话。 Secondly, as the note alludes to there is a versioning issue here. 其次,正如该说明所暗示的,这里存在版本问题。

C# enums are type safe meaning C#枚举是类型安全的含义

  1. You cannot implicitly convert from the underlying type to the actual enum 您不能隐式地从基础类型转换为实际的枚举
  2. Also, you cannot assign a value of one enum to another enum, even though the underlying types of both the enums are same. 此外,即使两个枚举的基础类型相同,也不能将一个枚举的值分配给另一个枚举。 An explicit cast is always required. 始终需要显式强制转换。
  3. Enums make your code more readable and more maintainable. 枚举使您的代码更具可读性和可维护性。

I have come accross an excellent video tutorial on enums exaplaining their type safe nature. 我已经通过一个关于枚举的优秀视频教程来探索他们的类型安全性。 Have a look here and there . 看看这里那里

Yes they are . 是的,他们是

Enums are strongly typed constants. 枚举是强类型常量。 They are essentially unique types that allow you to assign symbolic names to integral values. 它们本质上是唯一的类型,允许您将符号名称分配给整数值。 In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. 在C#传统中,它们是强类型的,这意味着即使其成员的基础值相同,也不能将一种类型的枚举隐式赋值给另一种类型的枚举。 Along the same lines, integral types and enums are not implicitly interchangable. 同样,整体类型和枚举也不是隐含地可互换的。 All assignments between different enum types and integral types require an explicit cast. 不同枚举类型和整数类型之间的所有赋值都需要显式强制转换。

Yes. 是。

C#: enum types: C#:枚举类型:

-A type-safe enumeration of named values. - 命名值的类型安全枚举。

-Prevents programming errors - 防止编程错误

-User can control underlying type (defaults to int) -User可以控制底层类型(默认为int)

-Also can control underlying values - 也可以控制基础价值

Technically no because you can represent an Enum as its base value (int, long, etc). 技术上没有,因为你可以将Enum表示为其基值(int,long等)。 However, if you ensure that you only use the enum by its provided names, you will receive compile time errors if you change the name of an enumerated value without updating its references. 但是,如果确保仅使用其提供的名称使用枚举,则在未更新其引用的情况下更改枚举值的名称时,将收到编译时错误。 In this regard yes it is type safe. 在这方面是的,它是类型安全的。

I'm late to the party here, but I wanted to throw out a little something extra ... An update to the .NET Framework Design Guidelines from Krzysztof Cwalina. 我在这里参加派对的时间已经很晚了,但是我想再多花点一些东西了...来自Krzysztof Cwalina的.NET Framework设计指南的更新。 In addition to the excellent tip above on checking to ensure a valid value is passed to your Enums, this provides other guidance and advice for how to use them effectively, a bunch of gotchas (especially involved Flags enums), etc. 除了检查以确保将有效值传递给您的Enums上面的优秀提示之外,这还提供了有关如何有效使用它们的一些指导和建议,一堆陷阱(特别是涉及Flags枚举)等。

yes they r strongly-typed safed u cannot it does not do impilcit convertion of enum constant variables to integeral value u hv 2 expilcitly do dat eg enum days { sun, mon } int e = (int ) days.sun; 是的,他们是强类型的安全你不能不做impilcit转换枚举常量变量到整数值u hv 2 expilcitly do dat例如enum days {sun,mon} int e =(int)days.sun; console.writeline(e); console.writeline(E);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM