c# 用枚举声明属性和字段,防止非法值,枚举 类型转换

 /// <summary>
        /// 性别
        /// </summary>
        public enum Gender
        {
            Male=0, Female=1
        }

        private Gender studentGender;
        public Gender StudentGender
        {
            get { return studentGender; }
            set { studentGender = value; }
        }

 

//将枚举转换成字符串
            Console.WriteLine("性别的值是:{0}",this.StudentGender.ToString());
            //将字符串转换成枚举
            Console.WriteLine("性别的值是:{0}",(Gender)(Enum.Parse(typeof(Gender),"Female")));

默认情况下,第一个枚举符号的值是 0

 

posted @ 2019-03-20 16:24  橙-极纪元JJYCheng  阅读(154)  评论(0编辑  收藏  举报