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

后端 未结 24 1974
深忆病人
深忆病人 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:20

    I did something similar with a different enum. It's for a game and the player has the chance to toggle colors.

    public enum PlayerColor {
        Red = 0, Green, Blue, Cyan, Yellow, Orange, Purple, Magenta
    }
    
    public PlayerColor GetNextFreeColor(PlayerColor oldColor) {
    
        PlayerColor newColor = (PlayerColor)((int)(oldColor + 1) % 8);
        return newColor;
    }
    

    This solution worked for me.

提交回复
热议问题