In depth analysis of the definition and use of enumeration types in c
- 2020-05-19 05:32:11
- OfStack
introduce
Enumeration is a specified constant whose base type can be any integer except Char.
If the underlying type is not explicitly declared, Int32 is used.
Programming languages typically provide a syntax to declare an enumeration of a set of named constants and their values.
define
The default cardinality starts with O and can also be specified.
enum Days { Saturday=1, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
use
Colors myColors = Colors.Red;
string strColor=myColors.tostring();
int IntColor=(int)myColors ;
or
Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
with
Colors myColors = Colors.Red & Colors.Blue & Colors.Yellow;
traverse
foreach (string s in Enum.GetNames(typeof(Days)))
Response.Write(s + "--" + Enum.Parse(typeof(Days), s).ToString());
conversion
Colors mc=Colors Enum.Parse(typeof(Colors ), "red");
if (System.Enum.IsDefined(typeof(Days), "Monday"))
Days ds= (Days)Enum.Parse(typeof(Days), "Monday");
Example 2:
public enum NoticeType
{
Notice = 'A',
LabRule = 'H',
HotInformation = 'N',
Column = 'C',
All = '1',
Null = '0'
}
// New enumeration type
NoticeType noticeType1 = NoticeType.Column;
// Converts the enumeration type to string d="Column"
string d = noticeType1.ToString();
// Gets the cardinality of an enumerated type dd='C'
char dd = (char)noticeType1;
// Gets the corresponding enumeration type from the cardinality noticeType2 = NoticeType.Notice
//(NoticeType)'A'; Either way
NoticeType noticeType2 = (NoticeType)Char.Parse("A");
// Gets an enumerated type by name noticeType3 = NoticeType.Notice
NoticeType noticeType3 = (NoticeType)Enum.Parse(typeof(NoticeType), "Notice");