你怎么能用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关键字失败,第二个关键字失败.
我有以下代码:
// 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)
但是,我无法索引值.有更简单的方法吗?
或者我完全错过了什么!