Enum Operators - CSharp Custom Type

CSharp examples for Custom Type:Enum

Introduction

The operators that work with enums are:

=   ==   !=   <   >   <=   >=   +   -   ^  &  |   ~
+=   -=   ++  --   sizeof

The bitwise, arithmetic, and comparison operators return the result of processing the underlying integral values.

Addition is permitted between an enum and an integral type, but not between two enums.

Since an enum can be cast to and from its underlying integral type, the actual value it may have may fall outside the bounds of a legal enum member.

public enum Direction { Left, Right, Top, Bottom }
    
Direction b = (Direction) 12345;
Console.WriteLine (b);                // 12345

The bitwise and arithmetic operators can produce similarly invalid values:

public enum Direction { Left, Right, Top, Bottom }

 Direction b = Direction.Bottom;
 b++;                                  // No errors

Related Tutorials