C # enum type enum (i)

Source: Internet
Author: User


1. Enumerations are declared using the enum keyword, and are siblings of the class. Enumerations can be tied to classes or

Written in the class, cannot be written in the method.


2. Enumerations are value types that are implicitly inherited from System.Enum and cannot be modified manually.

The System.Enum itself is a reference type and inherits from System.ValueType.


3. enumeration itself generally does not add access modifiers, to add the words can only be internal

orpublic;

• Enumerations are implicitly sealed (sealed)and are not allowed to derive subclasses as base classes.


4. Enumeration inside the member can only be the default common static (public, Static), cannot

have access modifiers;


5. Each enumeration type has an underlying type, which can be any integral type, an enumeration element

The default base type is int.

• By default, the first enumerator has a value of 0, followed by the value of each enumerator

Increment by 1.

• Enumerators can have initializers that override default values. However, it is strongly recommended that the

Contains a constant with a value of 0.

· enumeration elements cannot be duplicated, and the values of enumerated elements can be repeated.

• The name of the enumerator cannot contain whitespace.


6. The comma after the last element is optional. Enumeration of the last colon is optional.


7. The underlying type specifies the storage size allocated for each enumerator. However, from the enum type

Conversions to an integral type need to be done with an explicit type conversion .


See the following code for details:


namespace enumtest01{    class program    {         static void main (String[] args)          {            //3.--------- --------------------------------------------------------------------------------------------    The          //*** base type specifies the storage size allocated for each enumerator. However, conversions from the  enum  type to the integral type need to be done with an explicit type conversion.             int a =  (int) Days1.fri;             console.writeline (a);           //Printing 6             console.writeline (DAYS1.FRI);   //Printing fri           &Nbsp; console.readkey ();        }         //4.---------------------------------------------------------------------------------------- ---------        //*** enumeration can be tied to a class or written in a class, and cannot be written in a method.         enum A : byte         {        }    }     //1.---------------------------------------------------------------------------------------------     //*** enumeration itself generally does not add access modifiers, to add can only be internal or public.    enum animal     //Here you can add access modifiers internal or public.     {        dog = 1,    The members inside the     //enumeration are public by default and cannot have other modifiers.         cat = 0,        pig = 1,         //pig = 0          }     //*** enumerations are implicitly sealed and are not allowed to derive subclasses as base classes.     //enum m : animal { }     //here will be an error, Because enumerations cannot derive subclasses as base classes. The member inside the     //*** enumeration can only be the default public static, the default is the Int32 type, and cannot have an access modifier.     //2.---------------------------------------------------------------------------------- -----------    //*** each enum type has an underlying type that can be any integer,  the default underlying type of the enumeration element is  int.    The location of the  //byte can be written:  byte, sbyte, short, ushort, int, uint, long, ulong. The default is int.    enum a : byte    {    if you don't write.  }    //*** By default, the value of the first enumerator is  0, followed by the value of each enumerator incremented  1. Example:     enum days1 { sat, sun, mon,&nbsp tue, wed, thu, fri };    //,sat  as  0,sun in the  Days1  enumeration   for  1,Mon  for  2, and so on. Enumerators can have initializers that override default values. such as:    enum days2 { sat = 1, sun, mon, tue,  wed, thu, fri };    //in the  Days2  enumeration, forces the sequence of elements from  1  instead of  0   Start, sat  for  1,Sun   2,Mon  for  3, and so on. However, it is strongly recommended that the enumeration contain a constant with a value of  0 .     //*** enumeration elements cannot be duplicated, and the values of enumerated elements can be repeated.     enum Days3    {         Sat = 0,        //Sat = 0,     //the error here is that you have defined a sat.  enumeration member that cannot be the same, but the values of the enumeration can be the same.         Sun = 1,         mon = 1,        tue,        wed,        thu =  10,        Fri,           //the comma after the last element is optional.     };               The last colon of the   //enumeration is optional.     //in the  Days3  enumeration, the value of  tue is 2,wed value is the value of 3,fri is 11. The name of the     //*** enumerator cannot contain whitespace. }


C # enum type enum (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.