简体   繁体   English

在有点枚举上覆盖int

[英]Overriding int on a bit enum

I saw some .NET 2.0 code which looked like this: 我看到了一些.NET 2.0代码,如下所示:

 public enum abc : int
 {
  value1 = 1,
  value2 = 2,
  value3 = 4
 }

etc... 等等...

Now I know the flags enum can be used for bit values (like above, right?) so you can do | 现在,我知道标志枚举可用于位值(如上面的,对吧?),因此您可以| "or" &, etc, but what is the reason for not using that? “或”&等,但是不使用它的原因是什么? What is the point in overriding int? 覆盖int有什么意义?

If I design an enum for choosing multiple values I would use the flags attribute and not override anything (what I don't understand). 如果我设计一个用于选择多个值的枚举,则将使用flags属性而不覆盖任何内容(我不理解的内容)。 What does the above approach net? 上面的方法有什么用?

Thanks 谢谢

It's not "overriding" int , it's indicating what type the enumeration is based on. 它不是“覆盖” int ,而是指示枚举所基于的类型。 The default is int so this actually isn't achieving anything, but in some cases you might want to base an enum on long to store more flags, or byte to save space if there will be very many instances of the enum around and it only has a few values, however the recommendation in the framework design guidelines is to use int unless you have a very good reason not to. 默认值为int因此实际上并没有实现任何目的,但是在某些情况下,如果枚举周围有很多实例,并且您可能希望将枚举基于long来存储更多标志,或者以byte为基础以节省空间具有一些值,但是框架设计指南中的建议是使用int除非您有很好的理由不这样做。

If this is intended to be a flags enumeration then it should be marked with [Flags] , and this appears to be the intention here, so the omission looks like a mistake. 如果打算将其用作标志枚举,则应将其标记为[Flags] ,这似乎是这里的意图,因此省略看起来像是一个错误。 You can still combine the values using the bitwise operators even if it isn't marked with [Flags] , but from memory I think you get a compiler warning (or it might be an FxCop warning, or possibly a ReSharper warning...). 即使未用[Flags]标记,您仍然可以使用按位运算符组合值,但是从内存中,我认为您会收到编译器警告(或者可能是FxCop警告,也可能是ReSharper警告...) 。

The ": int" is specifying what to use as the underlying type for enum values; “:int”指定用作枚举值的基础类型的内容; any integral type other than char can be used here. 此处可以使用除char以外的任何整数类型。 The default is int, so it is redundant in this case. 默认值为int,因此在这种情况下是多余的。

FlagsAttribute only tells the user that fields in this enum can be combined; FlagsAttribute仅告诉用户该枚举中的字段可以合并; it doesn't actually set the fields of the enum to "flaggable" values. 它实际上并没有将枚举的字段设置为“可标记”值。 This, you will have to do yourself, just like you have already. 这将需要您自己做,就像已经做过一样。

I don't see any benefit. 我看不出任何好处。 Probably lack of experience on the programmer's part. 程序员方面可能缺乏经验。

If you are asking the scenario for choosing specific values, I don't know a good example offhand, but I can imagine something like 如果您要询问选择特定值的情况,我不知道有什么好的例子,但我可以想象

enum Color = {
  Red = 0xFF0000,
  Green = 0x00FF00,
  Blue = 0x0000FF }

maybe being useful. 也许有用。

Setting the values on a non-Flags enum to use a 1-hot encoding just for the sake of using a 1-hot encoding doesn't really make any sense. 仅出于使用1-hot编码的目的,将非Flags枚举上的值设置为使用1-hot编码实际上没有任何意义。 I guess it might make sense if you thought you might make it a Flags enum in the future. 我想如果您认为将来可以将其设为Flags枚举可能很有意义。 Otherwise, the values should either be not overridden or assigned a value based on their meaning (eg. Color.Red = 0xFF0000). 否则,不应覆盖这些值或根据其含义分配一个值(例如Color.Red = 0xFF0000)。

In reference to the ": int" specifying the underlying type, if I saw that, I'd have a look around and see if they delcare all their enums that way. 关于指定基础类型的“:int”,如果看到的话,我将环顾四周,看看它们是否以这种方式删除所有枚举。 If so, they probably just learnt to write enums that way ( or maybe they are a hyper-explicit coder). 如果是这样,他们可能只是学会了以这种方式编写枚举(或者他们是一个超外显的编码器)。 If however, just this enum was declared this way, perhaps the programmer is trying to say something (that might have been better said with a comment) that it is critical that this has an underlying "int" type. 但是,如果只是以这种方式声明了这个枚举,则可能是程序员试图说出某些话(最好用注释来说明),这必须具有底层的“ int”类型。 Perhaps it is being binary serialised or deserialised somewhere. 也许它正在某个地方进行二进制序列化或反序列化。

As for the [Flags] attribute, as far as C# is concerned, you can use bitwise operators on any enum, not just those declared with [Flags]. 至于[Flags]属性,就C#而言,您可以在任何枚举上使用按位运算符,而不仅仅是用[Flags]声明的那些。 From your above example, without any [Flags], the following is totally legal: 从上面的示例中,没有任何[标记],以下内容完全合法:

abc myAbcValue = value1 | abc myAbcValue = value1 | value2; 值2;

The primary difference with respect to the [Flags] is in the ToString() and Parse() methods of Enum. 关于[Flags]的主要区别在于Enum的ToString()和Parse()方法。 Without [Flags], myAbvValue.ToString() will return "3", but with [Flags] it will return "value1, value2". 如果没有[Flags],myAbvValue.ToString()将返回“ 3”,但是如果有[Flags],它将返回“ value1,value2”。 Likewise, with the [Flags], 同样地,使用[标志]

abc yourAbcValue = (abc)Enum.Parse( typeof(abc), "value1, value2") ; abc yourAbcValue =(abc)Enum.Parse(typeof(abc),“ value1,value2”);

will set the underlying value of yourAbcValue to 3. 会将yourAbcValue的基础值设置为3。

Note that the IsDefined() method of Enum() does not allow for multiple values with [Flags] enums, so: 请注意,Enum()的IsDefined()方法不允许带有[Flags]枚举的多个值,因此:

Enum.IsDefined( typeof(abc), "value1, value2") Enum.IsDefined(typeof(abc),“ value1,value2”)

returns false regardless of the use of [Flags]. 无论是否使用[Flags],都将返回false。

Also note that [Flags] provides information for any reflection access, and other languages may treat the restriction on flagged enums differntly to normal enums. 还要注意,[Flags]提供有关任何反射访问的信息,其他语言可能会将标记的枚举的限制与正常枚举不同。

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

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