Enum addition vs subtraction and casting

前端 未结 3 1224
逝去的感伤
逝去的感伤 2020-12-03 13:33

Why does addition require a cast but subtraction works without a cast? See the code below to understand what I am asking

public enum Stuff
{
    A = 1,
             


        
3条回答
  •  不思量自难忘°
    2020-12-03 14:00

    The default values for enums are 0,1,2... so in this case the difference between two enums will create another enum (compare them first to make sure you are subtracting the smaller from the larger).

    Half the time addition will create a value too high to be a valid enum.

提交回复
热议问题