C # Enumeration

Source: Internet
Author: User

The members in the structure can be assigned values. What about enumeration... is a value, read-only
You can create a class as a structure in the following situations:
(1) If there are very few fields in a class, the total memory occupied by all fields shall not exceed 8 or 16 bytes;
(2) If all fields in a class are of the value type;


Enumeration is used when you need a finite integer sequence. You can use enumeration to limit the value to a specific number of values. You can also use the. Net type check system to identify invalid values during compilation. This is obviously much more readable than using integers directly, and it also reduces (almost eliminates) the possibility of error code.



Enumeration in C #
Enumeration is a value type used to declare a group of named constants.
(1) Enumeration Declaration: The enumeration declaration is used to declare a new Enumeration type.
Access rhetoric Enum enumeration name: Basic Type
{
Enumeration member
}
The basic type must be able to represent all enumeration values defined in this enumeration. Enumeration declaration can explicitly declare the byte, sbyte, short, ushort, Int, uint, long, or ulong types as the corresponding basic types. The enumeration declaration without explicitly declaring the basic type means that the corresponding basic type is int.
(2) Enumeration members
The enumerated member is the name constant of this enumeration type. Any two enumeration members cannot have the same name. Each enumerated member has an associated constant value. The type of this value is the basic type of enumeration. The constant value of each enumeration member must be within the range of the basic Enumeration type.
Example:
Public Enum timeofday: uint
{
Morning =-3,
Afternoon =-2,
Evening =-1
}
An error occurs during compilation because the common values-1,-2, and-3 are not in the range of the basic integer uint.
(3) enumeration member Default Value
The first enumerated member declared in the enumeration type has a zero Mo value.
The following enumerated Member values are obtained by adding 1 to the value of the previous enumerated member (in the text order. In this way, the added value must be within the range of values that can be expressed by the basic type. Otherwise, a compilation error occurs.
Example:
Public Enum timeofday: uint
{
Morning,
Afternoon,
Evening
}
The morning value is 0, the afternoon value is 1, and the evening value is 2.
(4) assign values to the display of enumeration members
Multiple enumerated members are allowed to have the same value.
The value of the enumerated member is not displayed. The value of the previous enumerated member is always + 1.
Example
Public Enum number
{
A = 1,
B,
C = 1,
D
}
The value of B is 2, and the value of D is 2.
Note: The enumerated values cannot exceed the basic type range. Otherwise, an error is reported.
(5) Conversion of enumeration types and Basic Types
The basic type cannot be implicitly converted to the enumeration type.
The enumerated type cannot be implicitly converted to the basic type.
Example:
Public Enum number
{
A,
B,
C,
D
}

Class Test
{
Public static void main ()
{
Int I = number. A; // error. Force type conversion (INT) number.
Number N;
N = 2 // error. Force type conversion (number) 2
}
}
(6) system. Enum type
The system. Enum type is the abstract base class of all enumeration types, and Members inherited from system. Enum are available in any enumeration type.
System. Enum is not an enumeration type. Instead, it is a class type and all enumeration types are derived from it.
System. Enum is derived from the system. valuetype type.
(7) Use Enumeration type
Using system;
Public Enum timeofday
{
Morning,
Afternoon,
Evening
}
Class Test
{
Static void writegreeting (timeofday)
{
Switch (timeofday)
{
Case timeofday. Morning:
Console. writeline ("good morning ");
Break;
Case timeofday. Afternoon:
Console. writeline ("Good afternoon ");
Break;
Case timeofday. Evening:
Console. writeline ("Good evening ");
Break;
}
}
Static void main ()
{
Writegreeting (timeofday. Morning );
Writegreeting (timeofday. Evening );
Writegreeting (timeofday. afternoon );
}
}

C # Enumeration

Related Article

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.