When I came here today, I opened msdn and glanced at the enumeration type in C ~~ Although I found that there are still some new ways to play this thing.
Http://msdn.microsoft.com/zh-cn/library/sbbt4032.aspx#Y791
The first is a brief introduction ,...... Skipped here. Is Enum easy to use ~
Define a simple enumeration type:
EnumDays {sat, sun, Mon, Tue, wed, Thu,
Fri}; // days. Sat = 0 at this time
, Followed by this increment.
You can also manually specify the start value:
EnumDays {sat = 1, sun, Mon,
Tue, wed, Thu, Fri}; // It starts from 1.
The implicit type of enumeration is int, for example, int x = (INT) days. Sat;
No problem. According to msdn, the implicit type of enumeration can be any numeric type other than Char ...... [Every enumeration
Type has an underlying type, which can be any integral type character t char.
]
The following example shows how to use a long integer as the implicit type of enumeration:
//
Keyword_enum2.cs
// Using long enumerators
Using system;
Public class
Enumtest
{
Enum range: long {max =
2147483648l, min = 255l };
Static void
Main ()
{
Long x = (long)Range. Max;
Long y = (long)Range. min;
Console. writeline ("max = {0 }",
X );
Console. writeline ("min = {0}", y );
}
}
If you need to retrieve the long integer value again, it is also a conversion. Long x = (long)Range. Max;
The most interesting thing is the [flags] Mark of enum. Let's talk about nothing. Let's look at the program:
// The following code example extends strates the Use and effect of
// System. flagsattribute attribute on an Enum
Declaration.
// Enumflags. CS
// Using the flagsattriations on enumerations.
Using system;
[Flags]
Public Enum
Fileattribute
{
X01,
Hide =
0x02,
System = 0x04,
Archived = 0x08
}
Class flagtest
{
Static void main ()
{
Fileattributeoptions = fileattribute. readonly | fileattribute. system;
Console. writeline (options );
Console. writeline (INT) options );
}
}
Output:
Readonly, System
5
See it? Haha. This is a common flag ~
It becomes easier to use in C.
YesEnumDays {sat, sun, Mon, Tue, wed, Thu, Fri };
1> How do I return the corresponding sun in the form of a string given a value of 1?
A:Convert. changetype (enumvalue,
Enumtype). tostring (); // enumvalue = 1; enumtype = typeof (days)Thanks to lanyur,
The correct method should be (days) 1). tostring ();
2> how to return Enum day. Sun if I specify a string "sun?
A: You can use enum. parse (enumtype,
String, [Boolean]) to solve the problem. In this example, enum. parse (typeof (day), "Sun", true) returns day. Sun
, The first parameter specifies whether it is case sensitive. It can be omitted.
3> I want to know all the string values in Enum day. How do I write them?
A: this seems quite simple. foreach (string name in
Enum. getnames (typeof (day) console. writeline (name); there is also an enum. getname ()
For specific usage, go to msdn ....