的FlagsAttribute什么?FlagsAttribute

2023-09-05 02:29:12 作者:回忆在蔓延

在code波纹管

' no Flags'
Public Enum MyEnum
  Monday = 1
  Tuesday = 2
  Wednesday = 4
  Thursday = 8
End Enum

<Flags()> _ 
Public Enum MyEnum
  Monday = 1
  Tuesday = 2
  Wednesday = 4
  Thursday = 8
End Enum

我做的

Dim days As MyEnum = MyEnum.Monday Or MyEnum.Tuesday Or MyEnum.Wednesday 

If (days And MyEnum.Tuesday) = MyEnum.Tuesday Then
  Console.WriteLine("Tuesday OK") ' here'
Else
  Console.WriteLine("Tuesday NOK")
End If

If (days And MyEnum.Thursday ) = MyEnum.Thursday Then
  Console.WriteLine("Thursday OK")
Else
  Console.WriteLine("Thursday NOK") ' here'
End If

和获得相同的结果在两种情况下(有或没有FlagAttribute)。

and obtain the same result in both cases(with or without FlagAttribute).

推荐答案

基本上,它告诉了枚举值可以组合的CLR。如果没有这个属性,结合数值将导致一个未知的值(但它仍然是有效的)。与属性,组合PTED正确间$ P $

Basically, it tells the CLR that the values of the enum can be combined. Without this attribute, combining the values would result in an unknown value (but it would still be valid). With the attribute, the combination is correctly interpreted

没有标记属性:

' Gives "Monday, Tuesday" '
Dim s As String = (MyEnum.Monday Or MyEnum.Tuesday).ToString() 

没有标记属性:

' Gives "3" '
Dim s As String = (MyEnum.Monday Or MyEnum.Tuesday).ToString() 
相关推荐
 
精彩推荐
图片推荐