`
izuoyan
  • 浏览: 8879543 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

C#的Enum—枚举

 
阅读更多

枚举

  枚举类型声明为一组相关的符号常数定义了一个类型名称。枚举用于“多项选择”场合,就是程序运行时从编译时已经设定的固定数目的“选择”中做出决定。

  枚举类型(也称为枚举)为定义一组可以赋给 变量 的命名整数常量提供了一种有效的方法。例如,假设您必须定义一个变量 ,该变量 的值表示一周中的一天。该变量 只能存储七个有意义的值。若要定义这些值,可以使用枚举类型。枚举类型是使用 enum 关键字声明的。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};


  默认情况下,枚举中每个元素的基础类型是 int。可以使用冒号指定另一种整数值类型。
  如果不为枚举数列表中的元素指定 值,则它们的值将以 1 为增量自动递增。在前面的示例中,Days.Sunday 的值为 0,Days.Monday 的值为 1,依此类推。创建新的 Days 对象时,如果不显式为其赋值,则它将具有默认值 Days.Sunday (0)。创建枚举时,应选择最合理的默认值并赋给它一个零值。这便使得只要在创建枚举时未为其显式赋值,则所创建的全部枚举都将具有该默认值。枚举中大小 写敏感,但是建议不要这样。

枚举的优点

  <1>枚举可以使代码更易于维护,有助于确保给变量 指定合法的、期望的值。

  <2>枚举使代码更清晰,允许用描述性的名称表示整数值,而不是用含义模糊的数来表示。

  <3>枚举使代码更易于键入。在给枚举类型的实例赋值时,VS.NET IDE会通过IntelliSense弹出一个包含可接受值的列表框,减少了按键次数,并能够让我们回忆起可能的值

枚举实例

  声明:

  使用:


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> public string getTimeOfDay(TimeOfDaytime)
{
string result = string .Empty;
switch (time)
{
case TimeOfDay.Moning:
result
= " 上午 " ;
break ;
case TimeOfDay.Afternoon:
result
= " 下午 " ;
break ;
case TimeOfDay.Evening:
result
= " 晚上 " ;
break ;
default :
result
= " 未知 " ;
break ;
}
return result;
}

枚举方法

  <1>获取枚举字符串

TimeOfDaytime= TimeOfDay.Afternoon;

Console.WriteLine(time.ToString());
// 输出:Afternoon

  <2>Enum.Parse()方法。这个方法带 3 个参数,第一个参数是要使用 的枚举 类型。其语法是关键字 typeof 后跟放在括号中的枚举 类名。 typeof 运算符将在第 5 章详细论述。第二个参数是要转换的字符串,第三个参数是一个 bool ,指定在进行转换时是否忽略大小写。最后,注意 Enum.Parse() 方法实际上返回一个对象引用—— 我们需要把这个字符串显式转换为需要的枚举 类型 ( 这是一个取消装箱操作的例子 ) 。对于上面的代码,将返回 1 ,作为一个对象,对应于 TimeOfDay.Afternoon 的枚举 值。在显式转换为 int 时,会再次生成 1。

TimeOfDaytime2= (TimeOfDay)Enum.Parse( typeof (TimeOfDay), " afternoon " , true );

Console.WriteLine((
int )time2); // 输出1

  <3> 得到 枚举的某一值对应的名称


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->lbOne.Text = Enum.GetName( typeof (TimeOfDay), 0 );

  <4>得到 枚举的 所有的


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->foreach ( int i in Enum.GetValues( typeof (TimeOfDay)))
lbValues.Text
+= i.ToString();

  <5>枚举所有的 名称


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->foreach ( string temp in Enum.GetNames( typeof (TimeOfDay)))
lbNames.Text
+= temp;

枚举和常量

  优先考虑枚举。

  在C# 中, 枚举的真正强大之处是它们在后台会实例化为派生于基类System.Enum的结构。这表示可以对它们调用方法,执行有用的任务。注意因为.NET Framework的执行方式,在语法上把枚举当做结构是不会有性能损失的。实际上,一旦代码编译好,枚举就成为基本类型,与int和float类似。

  但是在实际应用中,你也许会发现,我们经常用英语定义枚举类型,因为开发工具本来就是英文开发的,美国人用起来,就直接能够明白枚举类型的含 义。其实,我们在开发的时候就多了一步操作,需要对枚举类型进行翻译。没办法,谁让编程语言是英语写的,如果是汉语写的,那我们也就不用翻译了,用起枚举 变得很方便了。举个简单的例子琓imeOfDay.Morning一看到Morning,美国人就知道是上午,但是对于中国的使用者来说,可能有很多人就 看不懂,这就需要我们进行翻译、解释,就向上面的getTimeOfDay()的方法,其实就是做了翻译工作。所以,在使用枚举的时候,感觉到并不是很方 便,有的时候我们还是比较乐意创建常量,然后在类中,声明一个集合来容纳常量和其意义。

  使用常量定义:这种方法固然可行,但是不能保证传入的参数day就是实际限定的。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->using System;
using System.Collections.Generic;

public class TimesOfDay
{
public const int Morning = 0 ;
public const int Afternoon = 1 ;
public const int Evening = 2 ;
public static Dictionary < int , string > list;
///
/// 获得星期几
///
///
///
public static string getTimeNameOfDay( int time)
{
if (list == null || list.Count <= 0 )
{
list
= new Dictionary < int , string > ();
list.Add(Morning,
" 上午 " );
list.Add(Afternoon,
" 下午 " );
list.Add(Evening,
" 晚上 " );
}

return list[time];
}
}

希望能够找到一种比较好的方法,将枚举转为我们想要的集合。搜寻了半天终于找到了一些线索。通过反射,得到 针对某一枚举类型的描述。

枚举的定义中加入描述


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->using System;
using System.ComponentModel;

public enum TimeOfDay
{
[Description(
" 上午 " )]
Moning,
[Description(
" 下午 " )]
Afternoon,
[Description(
" 晚上 " )]
Evening,
};

  获得值和表述的键值对


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> ///
/// 从枚举类型和它的特性读出并返回一个键值对
///
/// Type,该参数的格式为typeof(需要读的枚举 类型)
/// 键值对
public static NameValueCollectionGetNVCFromEnumValue(TypeenumType)
{
NameValueCollectionnvc
= new NameValueCollection();
TypetypeDescription
= typeof (DescriptionAttribute);
System.Reflection.FieldInfo[]fields
= enumType.GetFields();
string strText = string .Empty;
string strValue = string .Empty;
foreach (FieldInfofield in fields)
{
if (field.FieldType.IsEnum)
{
strValue
= (( int )enumType.InvokeMember(field.Name,BindingFlags.GetField, null , null , null )).ToString();
object []arr = field.GetCustomAttributes(typeDescription, true );
if (arr.Length > 0 )
{
DescriptionAttributeaa
= (DescriptionAttribute)arr[ 0 ];
strText
= aa.Description;
}
else
{
strText
= field.Name;
}
nvc.Add(strText,strValue);
}
}
return nvc;
}


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public enum TimeOfDay
{
Moning
= 0 ,
Afternoon
= 1 ,
Evening
= 2 ,
};
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics