【c#】枚举类型的简单使用


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

namespace unit5
{
    class Program
    {
        //枚举
        enum Week : short  //指定枚举类型中的值为short类型(默认情况下是 int 类型)
        {
            Monday = 1,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday,
            Sunday
        }
        static void Main(string[] args)
        {
            //枚举的使用
            byte byteDay;
            byte myByte;

            string myString;

            Week day = Week.Friday;

            byteDay = checked((byte)day);   //将枚举类型转换为byte类型
            Console.WriteLine(byteDay);

            //byte转换为枚举类型
            myByte = 3;   //值为3,映射过去显示Wednesday
            day = (Week)myByte;
            Console.WriteLine(day);
            myByte = 10;   //值为10,枚举类型可以存储这个值,但是在枚举中没有对这个值的映射,
                           //所以在后面的操作中可能出现逻辑上的错误,这样做 没有意义
            day = (Week)myByte;
            Console.WriteLine(day);

            //枚举转换为string
            myString = day.ToString();   //注意:不可以用(string)day 这样的转换,有更复杂的转换,即用 Enum.Parse()
            Console.WriteLine(day);
            //Console.WriteLine(day);  //显示的结果为Friday
            //day++;   //可以对枚举对象进行++操作
            //Console.WriteLine(day);

            Console.ReadKey();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值