One weekC #_The6Days
Enumeration
1Type
Value Type
LVariables directly contain their own data
LLocal variables are always placed on the stackStackMedium
Reference Type
NVariables indirectly point to their data
NPoint local variable to heapHeapObjects in
EnumerationEnum |
Value Type |
| structure struct |
Value Type |
| class class |
reference type |
| interface interface |
reference type |
| array [] array |
reference type |
| DeleGate DeleGate |
reference type |
C #Rules, suchInt,DoubleAnd other internal classes, belong to the structure,C #It is called "simple type ".
The biggest difference between a simple type and a custom type is that a simple type has a literal expression while a custom type does not.
Also, the third type is pointer. Pointers can only be used inUnsafeBlock (unsecureCode.
2Enumeration type
The enumerated type is a user-declared value type.
Enum suit
{
Clubs, diamonds, hearts, spades
} // SuitIndicates a deck,4Medium color: plum blossom, square, red heart, black peach
Sealed Class Example
{
Static void main ()
{
...
Suit lead = spades ;//Here Error
...
Suit trumps = suit. Clubs ;//This is correct.
...
}
}
Enumeration declaration:
Where the class declaration can appear
Including name, access permission, internal type, and enumeration members.
The scope of a variable is to define its enumeration.
Suit trumps = clubs ;//This is incorrect.ClubsLimitedSuitAs follows:
Suit trumps = suit. clubs;
3Enumeration considerations
The default enumerated value type isInt
You can select any internal Integer type.
But it cannot be a role type.
Enum suit: int //Internal type:Int, Can be omitted
{
Clubs,
Diamonds,
Heart = 42 ,//The default value of a member is the value of the previous Member.+ 1But you can also assign an initial value by yourself.
Spades ,//The semicolon of the last member is optional.
};//The end of the semicolon is optional.
Internal variables in the enumeration type can be explicitly declared types include:Sbyte, byte, short, ushort, Int, uint, long, ulong.
If no explicit declaration is made, the default type isInt.
The value of a member must be of the same type as that stated in the enumeration statement. Besides, it must be within the range of type.
If no value is assigned to a member, the value is the value of the previous Member.+ 1The default value of the first Member is1.
The enumerated members can be of the same value.
The last enumerated member can end with a comma.
The default access permission for enumeration members isPublic.
4Use Enumeration
The enumerated type is implicitly derived fromSystem. Enum
Namespace System
{
Public abstract class Enum
{
//Static Functions
Public static string [] getnames (type );
...
//Instance functions
Public override string tostring ();
//Constructor
Protected Enum ();
}
}
Suit trumps = suit. clubs;
String [] names = system. enum. getnames (trumps. GetType ());
...
Console. Write (Names [0]); // clubs
Console. Write (trumps); // clubs
Console. Write ("{0}", trumps); // clubs
Console. Write (trumps. tostring (); // clubs
Console. Write (suit) 24); // 24
System. EnumYesSystemAn abstract class of the namespace. It implements a series of interfaces:
Public abstract class Enum: icomparable, iformattable, iconvertible
{...}
Enumeration is implicitly derived fromSystem. Enum
System. EnumIt can only be used as an implicit base class of an enumeration type.
Cannot explicitly ExtractSystem. EnumDerive your own class.
Cannot createSystem. EnumClass.
5Enumeration Operator
Enumerated variables are treated as integer variables.
However, in most cases, the shift operator cannot be used.
6Enumeration Conversion
Implicit conversion
L0Can be converted to anyEnumType
LNever throw an error
Explicit Conversion
LSlaveEnumToEnumInternal type conversion
LSlaveEnumConvert to numeric type (includingChar)
LFrom numeric type (includingChar)Enum
LNever throw an error
0Can be converted to anyEnumType.EnumType included?0.