C#基础(二):enum与struct
作者:互联网
枚举类型
/* C#枚举体的应用 */ using System; namespace ConsoleApp1 { class Program { enum State { OnLine, OffLine, busy, cloaking }; static void Main(string[] args) { State st1 = State.busy; Console.WriteLine("State is : {0}", st1); //int转enum State st2 = (State)1; Console.WriteLine("State is : {0}", st2); //string转enum string str = "3"; State st3 = (State)Enum.Parse(typeof(State), str); Console.WriteLine("State is : {0}", st3); Console.ReadKey(); } } }结构体
C# 结构体的成员需要用属性说明,例如public, private。
代码示例:
using System; namespace 结构体 { //声明结构体 struct student { public string name; public Gender ge; public int age; public int id; } enum Gender { nan, nv } class Program { static void Main(string[] args) { student st1; st1.name = "张三"; st1.ge = Gender.nan; st1.age = 20; st1.id = 1000; Console.WriteLine("st1名字:" + st1.name); student st2; st2.name = "李小花"; st2.ge = Gender.nv; st2.age = 21; st2.id = 1001; Console.ReadKey(); } } }
标签:Console,struct,C#,enum,State,st1,st2,public,string 来源: https://blog.51cto.com/u_12570763/2847876