c # enumeration

Enumeration is a user-defined integer type, it is strongly typed, and similar classes, typically used to represent a set of constants. (Members can use Chinese characters)

public enum Sex: int // can specify types: byte, sbyte, Short, ushort, int, uint, Long, ulong 
        { 
            M = 0, // allowed to specify an integer range, the default is 0 to 1 in increments increasing 
            female = 1 
        }

Enumeration of advantages:

1, enables the enumeration code cleaner, which allows the use of descriptive names represents an integer value.

2, enumeration makes code easier to maintain, to help ensure that the specified variable legitimate, desired value.

3, to make the code easier to enumerate and read input.

1, a simple enumeration

  • Enumeration using the enum keyword to declare, with the same level of class. Enumeration itself may have a modifier, but enumeration members are always public, you can not have access modifiers. Enumeration itself modifiers can be used only public and internal.
  • Enumerations are value types implicitly inherit from System.Enum, can not be modified manually. System.Enum itself is a reference type, inherited from System.ValueType.
  • Implicit enumeration is sealed, allowed as base class subclasses.
  • Enumeration type enum members are static, and the default type is Int32.
  • Each enum member has a constant value associated with it. This value is the type of underlying data type enumeration. Constant value for each enum member must be within the underlying data type of the enumeration range. If you do not explicitly specify the underlying data type is the default data type is int.
  • Enum members can not be the same, but the enumeration value can be the same.
  • Enumeration last comma and the following members of the braces can be omitted semicolon

2, mark enumeration

To mark the enumeration at the top plus [System.Flags] characteristic statement. And enumeration support combined operations.

This bit arithmetic is very useful, there is also the sql statement support level operations. In other words, the results of a post-enumeration operations after stored in the database, you can also read out in accordance with the requirements. such as:

A "high handsome" stored as the value of the database stored in the database, it is stored to the integer 5.

the System the using; 
the using the System.Collections.Generic; 
the using the System.Linq; 
the using the System.Text; 
the using System.Threading.Tasks; 

namespace enumApplication 
{ 
    class Program 
    { 
        static void the Main (String [] args) 
        { 
            var = People man high |. . People Shuai; // calculation assigned 101 001 or 100, the result is 101 
            Console.WriteLine ( "enumeration value and high and handsome:" + (int) man); 
            IF ((man & People high. .) == people high) // 101 man 
            {// 001-by-phase high and, 
                Console.WriteLine ( "this man: tall"); // 001 if the result is high, you can launch man contains high anti 
            } 
            the else 
            {
                Console.WriteLine ( "person: low"); 
            } 
            the Console.ReadKey (); 
        } 

       [System.Flags] 
       public enum People: int 
        { 
            h = 1, 001 // 
            foo = 2, @ 010 
            handsome = 4 / / 100 
        } 
    } 
}

enumerate. Integer. Converting between strings

Sex enum public
{
male,
female
}

the Main void static (String [] args)
{
Console.WriteLine ((int) Sex F.); // convert enumerated int
Console.WriteLine (Sex Female .ToString ()); // convert enumerated String
Sex sex1 = (Sex) 1; // converts int enumeration
Console.WriteLine (sex1.ToString ());
Sex sex2 = (Sex) that Enum.Parse (typeof (Sex), "female"); // the string is converted to an enumeration
Console.WriteLine (sex2.ToString ());
Console.WriteLine (Enum.GetName (typeof (Sex),. 1)); // converts a number to a string corresponding to the enumeration
}

Guess you like

Origin www.cnblogs.com/1016391912pm/p/11353700.html