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).

  • 相关阅读:
    Javascript网页摇一摇
    移动端Web开发注意点
    Clappr——开源的Web视频播放器
    光看这图片就知道是大片--今天是五一劳动节尽管还是敲着代码(日常就是这样)然后想不出写什么了,也找不到好的素材,最后开心一下吧
    大放异彩的伪元素——可以做什么?(转)别人分享的文章,发现很不错,果断收藏了
    全屏滚动效果H5FullscreenPage.js
    今天我已无力吐槽了!写个没有营养的吐槽文。只是个人日记
    css的一些小技巧!页面视觉差!
    CSS3 transforms 3D翻开
    Javascript非构造函数的继承
  • 原文地址:https://www.cnblogs.com/chucklu/p/5438549.html
Copyright © 2011-2022 走看看