zoukankan      html  css  js  c++  java
  • enum类型

    enum (C# Reference)

    The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

    Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

    By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

    For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.

    enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

    Enumerators can use initializers to override the default values, as shown in the following example.

    enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

    In this enumeration, the sequence of elements is forced to start from 1 instead of 0. However, including a constant that has the value of 0 is recommended.

    For more information, see Enumeration Types (C# Programming Guide).

    Every enumeration type has an underlying type, which can be any integral type except char.

    The default underlying type of enumeration elements is int.

    To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example.

    enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

    The approved types for an enum are bytesbyteshortushortintuintlong, or ulong.

    A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.

    The default value of an enum E is the value produced by the expression (E)0.

    Note:

    An enumerator cannot contain white space in its name.

    The underlying type specifies how much storage is allocated for each enumerator.

    However, an explicit cast is necessary to convert from enum type to an integral type.

    For example, the following statement assigns the enumerator Sun to a variable of the type int by using a cast to convert from enum to int.

    int x = (int)Days.Sun;

    When you apply System.FlagsAttribute to an enumeration that contains elements that can be combined with a bitwise OR operation,

    the attribute affects the behavior of the enum when it is used with some tools.

    You can notice these changes when you use tools such as the Console class methods and the Expression Evaluator. (See the third example.)

    Enumeration Types (C# Programming Guide)

    enum MyType
        {
            A,
            B
        }
     MyType myType = MyType.A;
                    Console.WriteLine(myType);
                    int a = (int) myType;
                    Console.WriteLine(a);
                    int b = 3;
                    myType = (MyType) b;
                    Console.WriteLine(myType);

    上面的例子,MyType中A默认是0,B递增1; 

    b=3;是无法转换为MyType的,转换不会出错,打印myType,显示的是3

    The default value of enum

    https://stackoverflow.com/questions/4967656/what-is-the-default-value-for-enum-variable

    It is whatever member of the enumeration represents the value 0. Specifically, from the documentation:

    The default value of an enum E is the value produced by the expression (E)0.

    As an example, take the following enum:

    enum E
    {
        Foo, Bar, Baz, Quux
    }

    Without overriding the default values, printing default(E) returns Foo since it's the first-occurring element.

    However, it is not always the case that 0 of an enum is represented by the first member. For example, if you do this:

    enum F
    {
        // Give each element a custom value
        Foo = 1, Bar = 2, Baz = 3, Quux = 0
    }

    Printing default(F) will give you Quux, not Foo.

    If none of the elements in an enum G correspond to 0:

    enum G
    {
        Foo = 1, Bar = 2, Baz = 3, Quux = 4
    }

    default(G) returns literally 0, although its type remains as G (as quoted by the docs above, a cast to the given enum type).

  • 相关阅读:
    python类方法和静态方法
    42个创意户外广告设计
    50免费为移动设计和开发的PSD文件极力推荐
    40个高品质的免费商业PSD文件
    10 个有用免费 CSS3 强大工具
    10个方便的在线CSS代码生成器,网页设计师必备!
    对makefile中,变量定义中 通配符的理解
    GNU make manual 翻译(八十七)
    GNU make manual 翻译(八十九)
    GNU make manual 翻译(八十五)
  • 原文地址:https://www.cnblogs.com/chucklu/p/5438549.html
Copyright © 2011-2022 走看看