What Does the [Flags] Attribute Really Do?

前端 未结 6 1219
闹比i
闹比i 2020-12-06 03:58

What does applying [Flags] really do?

I know it modifies the behavior of Enum.ToString, but does it do anything else? (e.g. Different compiler or runtime behavior, e

6条回答
  •  离开以前
    2020-12-06 05:01

    Here's a list of concrete behavioral differences:

    • Setting an enum with [flags] to None clears all the flags.
    • The HasFlags method only works when this attribute is present.
    • As Devio said, it changes the capabilities of the Parse and Format methods. He linked to this article. Apparently it also impacts that is shown in Debuggers.
    • I thought [flags] probably had an impact on code generation in webservices, but it appears that this is not the case.
    • To be clear, bitwise operations are allowed on any enumeration, with or without [flags]. But using it is the best practice.

    More details: http://msdn.microsoft.com/en-us/library/ms229062.aspx

提交回复
热议问题