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

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

    Judging from your description, you don't really want an enum. You're stretching enum beyond its capabilities. Why not create a custom class that exposes the values you need as properties, while keeping them in OrderedDictionary. Then getting a next/previous one would be trivial. --update

    If you want to enumerate differently on the collection based in the context, make that explicit part of your design. Encapsulate the items within a class, and have few methods each returning IEnumerable where, T is your desired type.

    For example

    IEnumerable GetFoosByBar()
    IEnumerable GetFoosByBaz()
    

    etc...

提交回复
热议问题