枚举
使用
public enum Day
{
mon,
fri,
wes,
thr,
thu,
sat,
sun
}
类型转换
int转为枚举
#region//将int类型转为枚举
//int n1 = 1;
//Day dayss=(Day)n1;
//Console.WriteLine(dayss);
//Console.ReadKey();
#endregion
枚举转为int
#region//将枚举类型转为int类型
//Day days = Day.mon;
int n = 10;
//int n=(int)days;
//Console.WriteLine(n);
//Console.WriteLine((int)Day.fri);
//Console.ReadKey();
#endregion
string转为枚举
#region//将字符串转为枚举类型
//string num = "2";
//Day ss=(Day)Enum.Parse(typeof(Day), num);
//Console.WriteLine(ss);
//Console.ReadKey();
#endregion
枚举转为string
#region//将枚举类型转为string
//Day day = Day.mon;
//string num=day.ToString();
//Console.WriteLine(num);
//Console.ReadKey();
#endregion
小练习
public enum Day
{
mon,
fri,
wes,
thr,
thu,
sat,
sun
}
//提示用户选择一个在线状态,我们接受,并将用户的输入转换成枚举类型。
//再次打印到控制台中
Console.WriteLine("请输入1到6");
string num=Console.ReadLine();
switch (num)
{
case "1":Console.WriteLine((Day)Enum.Parse(typeof(Day), num));
break;
case "2":Console.WriteLine((Day)Enum.Parse(typeof(Day), num));
break;
case "3":Console.WriteLine((Day)Enum.Parse(typeof(Day), num));
break;
case "4":
Console.WriteLine((Day)Enum.Parse(typeof(Day), num));
break;
}
Console.ReadKey();
结构
public struct Person
{
public string _name;
public int _age;
public Gender _gender;
}
public enum Gender
{
boy,
girl
}
Person person;
person._name = "张三";
person._age = 1;
person._gender = Gender.boy;
Console.WriteLine("姓名:{0},年龄:{1},性别:{2}",person._name,person._age,person._gender);
Console.ReadKey();
外加简单数据类型的转换
显式类型转化
即强制类型转换。显式转换需要强制转换运算符,而且强制转换会造成数据丢失
Console.WriteLine("打印字符串喽1");
string num1=Console.ReadLine();
Console.WriteLine("打印字符串喽2");
string num2=Console.ReadLine();
Console.WriteLine("打印int喽");
int num3=int.Parse(Console.ReadLine());
int a=Convert.ToInt32(num1);
double b=Convert.ToDouble(num2);
Console.WriteLine(num3);
Console.WriteLine(a);
string c=b.ToString();
Console.WriteLine(c);
Console.ReadKey();
隐式类型转换
从小的整数类型转换为大的整数类型
int a = 10;
double b=(double)a;
Console.WriteLine(b);
Console.ReadKey();