枚举是C#中的值类型,它允许创建一种枚举类型,其中包含了一个固定集合,并能为变量提供集合中的值。
这个是自己的描述,不是很好,一般来说,面试会问枚举是值类型或是引用类型,这里搞错了就掉大了……
枚举的定义
1 public enum Dirction
2 
{
3 
   north,
4 
   west,
5 
   east,
6 
   south
7 }

这是最基本的定义方法
接着可以

1 void resDir()
2 
    {
3         //声明Dirction枚举,就像声明int一样。

4         Dirction dction = Dirction.east;
5         Response.Write(dction.ToString() + "<br>"
);
6         Response.Write(((int)dction).ToString() + "<br>"
);
7 
        
8 
出的结果是:
east
2
如果一个一个的试,结果是:
north
0
west
1
east
2
south
3
为什么会这样,我们换种枚举定义
1 public enum Dirctions
2 
    {
3         north=1
,
4         west=3
,
5 
        east,
6         south=15

7     }
这次我们一一输出
north
1
west
3
east
4
south
15
枚举的基本类型是整型,所以,就成这个样子?为什么要这个样子,我不能理解,但是这个不是我所能解决的了…
再看看,.net中常用的到的枚举是什么

 1 void getCommandTypeEnum()
 2 
    {
 3         //sql命令类型

 4         CommandType ct = CommandType.StoredProcedure;
 5         CommandType ct1 =
 CommandType.Text;
 6         //数据库链接状态

 7         ConnectionState cs = ConnectionState.Open;
 8         ConnectionState cs1 =
 ConnectionState.Closed;
 9 

10         ParameterDirection pd = ParameterDirection.Input;
11 

12