effective hierarchy(一)之 从array说起(2)

复习:

从上一节,可以看到2.0的数组有两个直接突破:a.从“原型数组”向引用类型的突破;b.嵌套数组元素间维度和容量可变.

 

数组暂时告一段落,进入与数组相关的下一个话题。

 

MSDN,C#2.0:

一、枚举关键字

1、基础

(1)枚举是指一个唯一的类型,它包含了一套名字常量,后者被称为枚举器列表(enumerator list);

(2)枚举使用关键字enum声明,语法为

        enum 枚举名[:类型名称]{枚举元素1,枚举元素2,枚举元素3...枚举元素n};

(3)枚举类型使用除char以外的任何基础数据类型(underlying type)进行存储;默认的是整型,表示为int32;

(4)缺省情况下,枚举元素列表的第一个元素是从数值0开始,随后的元素值递增幅度1;显式地为元素赋值,其值以所赋值为准;

(5)enum E的缺省值是表达式(E)0;

(6)与特性关联使用(此部分不感兴趣可跳过):当枚举的元素由位字段的或操作(bitwise OR operation)组成,并且使用System.FlagsAttribute时,enum关键字的行为受特性(attribute)的影响而发生变化,可以使用控制台类方法(console class method)、表达式计算器(expression evaluator)等写小程序观察。具体可见http://msdn.microsoft.com/en-us/library/system.flagsattribute(VS.80).aspx

 

2、注意事项

(1)枚举元素名不能包含空格;

(2)枚举元素的类型转换为整数类型,须进行显式转换;如int x = (int)Days.Sun;

 

3、建议(robust programming)

给枚举增加新值或改变枚举成员的值,可能会导致源代码依赖(dependant source code)问题,例如,在使用枚举的switch分支语句中。

 

//用途示例

// keyword_enum2.cs
// Using long enumerators

using System;
public class EnumTest 
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main() 
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

 

 二、Enum类型

(1)提供枚举的基类;语法为

 [SerializableAttribute]
 [ComVisibleAttribute(true)]
 public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
(2)Enum提供比较该类实例的方法、把实例值转换为字符串或反之、创建指定枚举的实例;

(3)可以把枚举视为位字段,这属于FlagsAttribute的范畴,不在此多说;

(4)枚举类型继承于值类型(ValuteType),实现了IComparable、IFormattable和IConvertible接口。使用Convert类自定义转换,以代替该类IConvertible的显式接口成员实现。

 

//用途示例
using System;

public class EnumTest {
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    enum BoilingPoints { Celsius = 100, Fahrenheit = 212 };
    [FlagsAttribute]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main() {

        Type weekdays = typeof(Days);
        Type boiling = typeof(BoilingPoints);

        Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:");

        foreach ( string s in Enum.GetNames(weekdays) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d"));

        Console.WriteLine();
        Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.");
        Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:");

        foreach ( string s in Enum.GetNames(boiling) )
            Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d"));

        Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
        Console.WriteLine();
        Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors);
    }
}

 

----------------------------------------------------------------------------------------------------

小结:枚举是属于数值类型,即在栈中创建。并且,相对于数组,它提供了方便地使用性。争论的焦点是它带有“硬代码”的特性,一般作为底层支撑字典应用。

你可能感兴趣的:(Microsoft,sun)