How to get next (or previous) enum value in C#

后端 未结 24 1989
深忆病人
深忆病人 2020-12-04 10:59

I have an enum which is defined like this:

public enum eRat { A = 0, B=3, C=5, D=8 };

So given value eRat.B, I want to get the

24条回答
  •  时光说笑
    2020-12-04 11:28

    I would go with Sung Meister's answer but here is an alternative:

    MyEnum initial = MyEnum.B, next;
    
    for (int i = ((int) initial) + 1, i < int.MaxValue; i++)
    {
      if (Enum.IsDefined(typeof(MyEnum), (MyEnum) i))
      {
         next = (MyEnum) i;
         break;
      }
    }
    

    Note: many assumptions assumed :)

提交回复
热议问题