为枚举(enum)类型添加的备注属性,并且获取枚举的属性值
枚举类型(也称为枚举)是非常有意思的的一种值类型,它不仅可以提高程序的可读性,而且可以减少因业务改变而导致的程序改动。
另外枚举类型是强类型,接受方法必须有一个相同的匹配参数,否则将会出错。
好,废话少说,至于枚举类型(emun)的特性,几乎会编程的人都懂的,偶没功力在这里长篇大论。
下面是一个例子,偶还是按官方的写法,以星期为例子:
代码
class Program
{
static void Main(string[] args)
{
Console.Write(GetDays(Days.Saturday));
Console.Read();
}
static string GetDays(Days _days)
{
string d = string.Empty;
switch (_days)
{
case Days.Monday: d = "Today is Monday."; break;
case Days.Tuesday: d = "Today is Tuesday."; break;
case Days.Wednesday: d = "Today is Wednesday."; break;
case Days.Thursday: d = "Today is Thursday."; break;
case Days.Friday: d = "Today is Friday."; break; ;
case Days.Saturday: d = "Today is Saturday."; break;
case Days.Sunday: d = "Today is Sunday."; break;
}
return d;
}
}
public enum Days
{
/// <summary>
/// this is sunday
/// </summary>
Sunday = 7,
/// <summary>
/// this is monday
/// </summary>
Monday = 1,
/// <summary>
/// this is tuesday
/// </summary>
Tuesday = 2,
/// <summary>
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,
部分文章来自网络,