How to enumerate an enum

后端 未结 29 1893
深忆病人
深忆病人 2020-11-22 01:14

How can you enumerate an enum in C#?

E.g. the following code does not compile:

public enum Suit
{         


        
29条回答
  •  臣服心动
    2020-11-22 01:56

    foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit)))
    {
    }
    

    Note: The cast to (Suit[]) is not strictly necessary, but it does make the code 0.5 ns faster.

提交回复
热议问题