Search C # (enumerated 2) a week
C # Talent Bird (QQ: 249178521)
4. Use enumeration
Enumeration is derived from system.enum
Namespace System
{
Public Abstract Class Enum ...
{
// Static function
Public static string [] getNames (type);
...
// instance function
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.enum is an abstract class in the System namespace. It implements a series of interfaces:
Public Abstract Class enum
: IComparable, IFORMATTABLE, ICONVERTIBLE
{
...
}
Enumeration (such as Suit) is implicitly derived from System.enum.
System.Enum is a special class that can only be used as an implicit base class for an enumeration type.
You can't explicitly derive your own class from System.enum.
You can't create an instance of system.enum. (It constructor is protected and it is an abstract class).
5. Enumeration operator
· Enumeration variables are treated as an integer variable, but the shift operator cannot be used in most cases.
6. Enumeration conversion
· Implicit conversion
W 0 can be converted to eNUM type
W never throws an error
· Explicit conversion
W from Enum to ENUM through the inner type conversion
W is converted from enum to numerical types (including char)
W from numeric type (including char) to ENUM
W never throws an error
0 can be converted to eNUM type, regardless of the ENUM type package does not include 0.
If you use console.writeline to see an enumeration value, it seems to be implicit to a string. But this is an illusion, the following example shows this:
ENUM Suit {Clubs, Diamonds, Hearts, Spades}
Suit trumps = suit.clubs;
Console.writeline (Trumps); // Visual as Clubs
String s = trumps; // error, trumps is not a string
Console.WriteLine completed by the type conversion from Enum to String is implemented via system.enum's iFormatTable interface.