Why are operations between different enum types allowed in another enum declaration but not elsewhere?

前端 未结 4 551
太阳男子
太阳男子 2021-02-12 12:26

The C# compiler allows operations between different enum types in another enum type declaration, like this:

public enum VerticalAnchors
{
    Top=1,
    Mid=2,
          


        
4条回答
  •  无人及你
    2021-02-12 13:00

    C# allows definition of enum values to contain constant value expression, so that an enum value can be a combination of enumerations, e.g. [Flags]. The compiler evaulates each enum value in the expression as int (usually), thus you can do bitwise and arithmetic operations on the enum value.

    Outside of an enum definition, you must cast the enum to a primitive type before performing an operation on it.

提交回复
热议问题