超紧急C#枚举的问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 14:46:47
namespace ConsoleApplication13
{
class program
{
public enum A
{
red,
blue,
green=-1,
black,
yellow
}
static void Main()
{
Console.WriteLine(A.red);
}
}
}
为什么输出的不是red,而是black?

Console.WriteLine(A.red + "=" + (int)A.red);
Console.WriteLine(A.blue + "=" + (int)A.blue);
Console.WriteLine(A.green + "=" + (int)A.green);
Console.WriteLine(A.black + "=" + (int)A.black);
Console.WriteLine(A.yellow + "=" + (int)A.yellow);
结果是:

black=0
blue=1
green=-1
black=0
blue=1

明白了吧?

改成这样:

namespace ConsoleApplication13
{
class program
{
public enum A
{
red=-3,
blue,
green,
black,
yellow
}
static void Main()
{
Console.WriteLine(A.red);
}
}
}

namespace ConsoleApplication13
{
class program
{
public enum A
{
red=,
blue,
green=-1,
black=-2,
yellow
}
static void Main()
{
Console.WriteLine(A.red);
}
}