相关疑难解决方法(0)

如何在C#中枚举枚举?

你怎么能用enumC#枚举一下?

例如,以下代码无法编译:

public enum Suit 
{
    Spades,
    Hearts,
    Clubs,
    Diamonds
}

public void EnumerateAllSuitsDemoMethod() 
{
    foreach (Suit suit in Suit) 
    {
        DoSomething(suit);
    }
}
Run Code Online (Sandbox Code Playgroud)

并给出以下编译时错误:

'Suit'是'type',但用作'变量'

Suit关键字失败,第二个关键字失败.

.net c# enums loops enumeration

3620
推荐指数
28
解决办法
73万
查看次数

C#通过枚举迭代?(索引System.Array)

我有以下代码:

// Obtain the string names of all the elements within myEnum 
String[] names = Enum.GetNames( typeof( myEnum ) );

// Obtain the values of all the elements within myEnum 
Array values = Enum.GetValues( typeof( myEnum ) );

// Print the names and values to file
for ( int i = 0; i < names.Length; i++ )
{
    print( names[i], values[i] ); 
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法索引值.有更简单的方法吗?

或者我完全错过了什么!

c# iteration enums system.array

109
推荐指数
5
解决办法
12万
查看次数

标签 统计

c# ×2

enums ×2

.net ×1

enumeration ×1

iteration ×1

loops ×1

system.array ×1