Do C # enum flags needed

In C #, should enum flags be sequential? or can you leave spaces? and still do bitwise comparisons? those. can you do the following:

[Flags] public enum MyEnum { None = 0, IsStarred = 1, IsDone = 128 } 
+6
source share
8 answers

There is nothing wrong with the code you posted. This is absolutely normal:

 [Flags] public enum MyEnum { None = 0, IsStarred = 1, IsDone = 128 } 

And like this:

 [Flags] public enum MyEnum { IsStarred = 1, IsDone = 128 None = 0, SomethingElse = 4, } 

Just remember that FlagsAttribute does not apply your values ​​as bit masks.

+5
source

There is nothing that would require them to be consistent.

Your listing definition is beautiful and will compile without problems.

The problem of readability and the principle of least surprise, however, were significantly compromised ...

+7
source

There is no such requirement. You are fine if you use [Flags] .

+4
source

They do not have to be consistent.

+4
source

Yes you can do it. It depends on you.

+3
source

No, they do not have to be consistent. Compile your code and see for yourself.

+1
source

You can not only do this, but also do this:

 public enum MyEnum { None, IsStarred, IsDone = 128 } 

or

 public enum MyEnum { None = 5, IsStarred, IsDone = 128 } 

here is a link to other examples: http://www.dotnetperls.com/enum

+1
source

Reference enumerations must not have the Flags attribute. But this is the best practice. You can read here: http://msdn.microsoft.com/en-us/library/ms229062.aspx

0
source

Source: https://habr.com/ru/post/907882/


All Articles