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, } 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, } 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 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"; ToString is GrundySource: https://ru.stackoverflow.com/questions/585876/
All Articles