第9章 枚举类型和结构体
enum 枚举名{
val1,
val2
}
enum game
{
夏侯惇,
不知火舞,
李元芳
}
game shanglu = game.夏侯惇;
game zhonglu = game.不知火舞;
game xialu = game.李元芳;
//每个枚举中的值都对应一个整数默认从0开始,所以枚举类型可以和整型类型互相转换
Console.WriteLine(Convert.ToInt32(xialu));
//枚举类型也可以和字符串进行转换
Console.WriteLine(Convert.ToSingle(xialu));
//遍历枚举中的名字,Enum类中的GetValues方法,获取枚举的值
foreach (string i in Enum.GetNames(typeof(game))) {
Console.WriteLine(i);
}
struct student {
private int id;
//public修饰,这样才可以被公开访问
public string name;
public int age;
public string printNum() { return "hello"; }
}