初学C#,问个关于枚举的简单问题
代码:
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
struct strtest
{
public string name;
public int age;
}
public enum enumWeek
{
Sunday = 0,
Monday = 1,
mycount = 200
}
static void Main(string[] args)
{
strtest st = new strtest();
st.name = "xiaojie";
st.age = 20;
strtest st1;
st1 = st;
int i = 200;
switch (i)
{
case (int) enumWeek.Sunday:
Console.WriteLine("输出0");
break;
case (int) enumWeek.Monday:
Console.WriteLine("输出1");
break;
case (int)enumWeek.mycount:
Console.WriteLine("输出200");
break;
default:
Console.WriteLine("没有输出");
break;
}
int myco = 200;
int total;
Console.WriteLine(enumWeek.mycount); //不明白为什么不输出200而输出mycount;
//total = myco + enumWeek.mycount; //这样会出错
}
}
初学C#,也看了关于枚举的定义,以上代码纯为自己测试。我不明白书上说枚举其实是一个整数类型,但我直接输出Console.WriteLine(enumWeek.mycount)不会输出它的值而是输出mycount,并且我用它直接和其它整型变量相加会出错,但在switch中用就能正确识别,不会出错。这有什么区别吗?为什么会这样?