码迷,mamicode.com
首页 > Windows程序 > 详细

C# 枚举类型

时间:2019-01-12 22:47:46      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:col   ons   linq   sys   end   创建   thread   定义   枚举   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 为什么枚举是递增的整数类型? Gender.女等价于(Gender)1
            Class1 sex = new Class1(Gender.女);  // 实例化不能使用字符串
            sex.showSex();
        }
    }
}

Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Class1
    {
        // 枚举类型的使用
        private Gender sex = Gender.男;  // 默认不能再使用字符串
        public Class1(Gender sex)
        {
            this.sex = sex;
        }
        public void showSex()
        {
            Console.WriteLine(this.sex);
        }
    }
}

Gender.cs

// 性别
/*
 * 枚举属于值类型,不能在枚举值定义属性和方法
 * 枚举是从0开始递增的整数类型
 * 枚举类型的创建:在项目文件选择"添加"->"新建项"->"代码文件",写入代码如下:
 */
enum Gender
{
    男,女
}

 

C# 枚举类型

标签:col   ons   linq   sys   end   创建   thread   定义   枚举   

原文地址:https://www.cnblogs.com/namejr/p/10261218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!