的FlagsAttribute什么? [英] FlagsAttribute what for?

查看:156
本文介绍了的FlagsAttribute什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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() 

这篇关于的FlagsAttribute什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆