What is the difference between the listing declared as follows:

[Flags] public enum ASD { None = 0, Param1 = 1, Param2 = 2, Param3 = 4, } 

or so

 public enum ASD { None = 0, Param1 = 1, Param2 = 2, Param3 = 4, } 

2 answers 2

This attribute means that the values ​​of the enumeration to which it is applied can be considered as bit fields and bit operations can be applied to them, which will affect, in particular, the behavior of the ToString method.

For example:

 // если перечисление помечено атрибутом ASD flags = (ASD)5; Console.WriteLine(flags); // выведет Param1, Param3 // если перечисление не помечено атрибутом ASD enums = (ASD)5; Console.WriteLine(enums); // выведет 5 
  • Bit operations can be applied to the usual enumeration, I just checked. Well, do not exist bit flag just like that. - Align
  • 2
    @Prisoner is possible, only the behavior will be somewhat different. I added an example - DreamChild
  • Console.WriteLine (flags.ToString ("F")); // also displays Param1, Param3 regardless of the presence of the attribute [Flags] - Vitaliy

From the point of view of the mechanism of work - this attribute changes the behavior of the ToString() method

Without attribute:

 ((ASD)3).ToString() == "3"; 

With attribute:

 ((ASD)3).ToString() == "Param1, Param2"; 
  • one
    eeeeh :-) I type the text too long :-D - Grundy
  • This attribute does not seem to affect static methods, only ToString is Grundy
  • @Grundy hmmm, funny - but yes. Parse parsit lists of values ​​even for ordinary enumerations ... - Pavel Mayorov
  • @Grundy by the way, I added this after I saw your comment :) - Pavel Mayorov
  • Yeah, that's because there was a comment quotation, and then they say to him there that you’re not quite right with these methods :) - Grundy