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

  • 相关阅读:
    Jenkins安装部署及使用
    Jenkins详细安装与构建部署使用教程
    线程池使用拒绝策略时需要注意的坑
    线程池的4种拒绝策略
    neo4j allshortestpaths查询路径不准确问题
    程序员必备的网站之Tutorialspoint
    All shortest paths between a set of nodes
    Neo4j/Cypher: All paths between two nodes with a relationship property filter
    12款好看的英文字体下载《可以免费用于商业用途》
    国外经典设计:12个漂亮的移动APP网站案例
  • 原文地址:https://www.cnblogs.com/chucklu/p/5438549.html
Copyright © 2011-2022 走看看