简体   繁体   中英

Flags Enum attribute

没有它你可以测试的[Flags]属性有什么意义?

The Flags attribute allows you to see a CSV(comma separated value) of your enumerated type when calling ToString()

For Example:

[Flags]
public Enum Permissions
{
  None =0,
  Read = 1,
  Write =2,
  Delete= 4
}

Permissions p = Permissions.Read | Permissions.Write;
p.ToString() //Prints out "Read, Write"

However you can still get the same thing if you remove the flags attribute and just do:

p.ToString("F") //Prints out "Read, Write"

And as John pointed out it also allows you convert a CSV back to Enum using Enum.Parse

它改变了字符串和枚举值( Enum.ParseToString )之间转换的行为。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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