本文以C#实例讲解了枚举类型与结构类型的用法,程序主要是通过个人电话本演示枚举类型与结构类型的用法,具体代码如下所示:
using System; class ID { //定义枚举类型 public enum Sex { male, female };//注意别忘了这里的分号 //定义电话本的结构类型 public struct TelBook { public string name; public Sex sex;//性别类型为枚举类型 public string number; } //每一行打印一位用户的电话本信息 public static void TelPrint( TelBook Someone ) { Console.Write( Someone.name + "\t"); Console.Write( Someone.sex + "\t"); Console.Write( Someone.number + "\r\n"); } public static void Main() { TelBook Joey, Rose;//声明TelBook结构类型两位用户Joey和Rose Joey.name= "Joey"; //初始化Joey电话本信息 Joey.sex= Sex.male; Joey.number = "84113128"; Rose.name = "Rose";//初始化Rose电话本信息 Rose.sex = Sex.female; Rose.number = "84117456"; TelPrint ( Joey ); //打印两位用户的电话本 TelPrint ( Rose ); } }