C#枚举类型学习之基础讲解

对于C#枚举类型不仅可以提高程序的可读性,而且可以减少因底层值发生改变而导致的程序改动。另外一个好处是枚举类型是强类型,以enum类型作为参数传递时,接受方法必须有一个相同的匹配参数;否则编译器将会报错。

成都创新互联从2013年开始,是专业互联网技术服务公司,拥有项目做网站、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元白山做网站,已为上家服务,为白山各地企业和个人服务,联系电话:028-86922220

C#枚举类型的基础类型可以是除Char外的任何整型。如果没有显式声明基础类型,则使用Int32。如果没有为enum符号赋之,系统会自动对其分别赋值为0,1,2,3,等等。

如果要将C#枚举类型赋值给基本类型,则需要显式强制转换,如

 
 
 
  1. intseven=(int)Week.Sunday; //seven=7   

下面是一个C#枚举类型例程,解释使用enum怎样使程序更加清晰易读:

 
 
 
  1. enumWeek:int{  
  2. Monday =1;  
  3. Tuesday=2;  
  4. Wednesday=3;  
  5. Thursday=4;  
  6. Friday=5;  
  7. Saturday=6;  
  8. Sunday=7;  
  9.  }  
  10.    
  11.  staticstringGetDay(Weekday)  
  12. {  
  13.  caseWeek.Monday:return("TodayisMonday.");  
  14.  caseWeek.Tuesday:return("TodayisTuesday.");   
  15.  caseWeek.Wednesday:return("TodayisWednesday.");  
  16.  caseWeek.Thursday:return("TodayisThursday.");   
  17.  caseWeek.Friday:return("TodayisFriday.");   
  18.  caseWeek.Saturday:return("TodayisSaturday.");   
  19.  caseWeek.Sunday:return("TodayisSunday.");   
  20.  default:return("nosuchday");  

C#枚举类型学习之System.Enum的方法

System.Enum中三个比较有用的方法是Enum.IsDefined、Enum.Parse和Enum.GetName。

这三个方法都是staticmethod,前两种方法常一起使用,用来确定一个值或符号是否是一个枚举的成员,然后创建它的一个实例。

IsDefined方法有两个参数:一个是typeof操作符返回的枚举类型,另一个表示所测试的字符串。如果传递一个数字之作为第二个参数,这是这个方法的第二种形式,用于测试是否有指定的常量。

Parse方法选取同样的参数,并创建枚举类型的一个实例。在使用Parse方法之前,一定要确保该枚举成员已经存在,否则系统会抛出一个异常。

GetName方法根据指定值(作为第二个参数传入)返回枚举中的相应字符串。如

 
 
 
  1. stringtues=Enum.GetName(typeof(Week),2);  
  2.  
  3. tues=Tuesday 

这里有一个C#枚举类型实例,用来确定是否包含于给定字符串值匹配的符号。如果有,则创建此enum的一个实例,并使用方法GetName打印出其中的一个成员值。

关于Enum的toString方法

这里有一个我在CSDN上看到的程序,读懂这个程序,不仅可以很好的理解关于Enum的toString方法,而且可以很好的理解符号和值之间的关系。

 
 
 
  1.  using System; class Sample   
  2.  {  
  3.  enum Colors {Red,   
  4.  
  5. Green, Blue, Yellow}; public static void Main()   
  6.  {  
  7.  Colors myColor = Colors.Yellow;   
  8. Console.WriteLine("Colors.Red = {0}",   
  9. Colors.Red.ToString("d"));  
  10.  Console.WriteLine("Colors.Green = {0}",   
  11. Colors.Green.ToString("d"));   
  12. Console.WriteLine("Colors.Blue = {0}",   
  13. Colors.Blue.ToString("d"));  
  14.  Console.WriteLine("Colors.Yellow = {0}",  
  15.  Colors.Yellow.ToString("d"));   
  16. Console.WriteLine("{0}myColor = Colors.Yellow{0}",  
  17.  Environment.NewLine);   
  18. Console.WriteLine("myColor.ToString("g") = {0}",   
  19. myColor.ToString("g"));  
  20.  Console.WriteLine("myColor.ToString("G") = {0}",  
  21.  myColor.ToString("G"));   
  22. Console.WriteLine("myColor.ToString("x") = {0}",  
  23.  myColor.ToString("x"));  
  24.  Console.WriteLine("myColor.ToString("X") = {0}",   
  25. myColor.ToString("X"));   
  26. Console.WriteLine("myColor.ToString("d") = {0}",   
  27. myColor.ToString("d"));  
  28.  Console.WriteLine("myColor.ToString("D") = {0}",  
  29.  myColor.ToString("D"));  
  30.  Console.WriteLine("myColor.ToString("f") = {0}",  
  31.  myColor.ToString("f"));  
  32.  Console.WriteLine("myColor.ToString("F") = {0}",  
  33. myColor.ToString("F"));  
  34.  }  
  35.  }   //C#枚举类型
  36. /*  
  37.  This example produces the following results:  
  38.  Colors.Red = 0  
  39.  Colors.Green = 1  
  40.  Colors.Blue = 2  
  41.  Colors.Yellow = 3   
  42.  
  43. myColor = Colors.Yellow myColor.ToString("g") = Yellow  
  44.  myColor.ToString("G") = Yellow  
  45.  myColor.ToString("x") = 00000003  
  46.  myColor.ToString("X") = 00000003  
  47.  myColor.ToString("d") = 3  
  48.  myColor.ToString("D") = 3  
  49.  myColor.ToString("f") = Yellow  
  50.  myColor.ToString("F") = Yellow  
  51.  */  

Enum.ToString 方法 ()

C#枚举类型返回值

此实例的值的字符串表示。

C#枚举类型备注

使用此方法就如同指定了通用格式字符“G”一样。也就是说,如果未将 FlagsAttribute 应用到此枚举类型,且存在与此实例的值相等的已命名常数,则返回值为包含该常数名称的字符串。如果应用了 FlagsAttribute,且存在与此实例的值相等的一个或多个已命名常数的组合,则返回值是一个字符串,该字符串包含用分隔符分隔的常数名称列表。其他情况下,返回值是此实例的数值的字符串表示形式。

有关格式字符的更多信息,请参见 Format 方法的备注部分。有关一般格式化的更多信息,请参见格式化概述。

.NET Framework 精简版 - Windows CE .NET 平台说明:因为此方法搜索元数据表,所以它大量占用系统资源,从而可能影响性能。

C#枚举类型示例

 
 
 
  1.  using System; public class EnumSample {  
  2.  enum Colors {Red = 1, Blue = 2};  
  3.  
  4.  public static void Main() {  
  5.  Enum myColors = Colors.Red;  
  6.  Console.WriteLine("The value of this instance is ’{0}’",  
  7. myColors.ToString());  
  8.  }  
  9.  }  
  10.  /*  
  11.  Output.  
  12.  The value of this instance is ’Red’.  
  13.  */  

C#枚举类型之枚举和位标志

我们经常会把枚举类型的值设置为2的幂值,这是因为枚举成员经常要做逻辑操作,在这种情况下,这种2的幂值由一个显著的优点,即它们可以映射到某个二进制位。下面给出一个例子:

 
 
 
  1. enumfabric  
  2. {  
  3. cotton=1,  
  4. silk=2,  
  5. wool=4,  
  6. rayon=8,  
  7. other=128,  
  8. }  
  9. fabricfab=fabric.cotton|fabric.wool;  
  10. Console.WriteLine(fab.ToString());   
  11.  
  12. //output:5  

如果输出结果能把变量表示为wool和cotton的组合,就会更有意思。通过在枚举中添加[flags]属性就可以做到。

 
 
 
  1. [Flags]  
  2. enumfabric  
  3. {  
  4. cotton=1,  
  5. silk=2,  
  6. wool=4,  
  7. rayon=8,  
  8. other=128,  
  9. }  
  10. fabricfab=fabric.cotton|fabric.wool;  
  11. Console.WriteLine(fab.ToString("g"));//output:cotton,wool  

C#枚举类型的基本就向你介绍到这里,希望对你了解和学习C#枚举类型有所帮助。

网站名称:C#枚举类型学习之基础讲解
网站路径:http://www.shufengxianlan.com/qtweb/news22/22772.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联