This article from: http://tech.ddvip.com/2007-11/119553764137735.html
Content summary: The Enumeration type in C # can not only improve the readability of the program, but also reduce program changes caused by changes in the underlying value.
The enumeration type in C # not only improves the readability of the program, but also reduces program changes caused by changes in the underlying value. Another advantage is that the enumeration type is strongly typed. When the enum type is passed as a parameter, the accept method must have the same matching parameter; otherwise, the compiler will report an error.
The basic type of enumeration can be any integer except char. If the basic type is not explicitly declared, int32 is used. If the enum symbol is not assigned, the system automatically assigns the enum to 0, 1, 2, 3, and so on.
If you want to assign an enumeration type to a basic type, you need to explicitly forcibly convert it, as shown in figure
intseven=(int)Week.Sunday; //seven=7
The following is a routine that explains how to use Enum to make the program clearer and easier to read:
enumWeek:int{
Monday =1;
Tuesday=2;
Wednesday=3;
Thursday=4;
Friday=5;
Saturday=6;
Sunday=7;
}
staticstringGetDay(Weekday)
{
caseWeek.Monday:return("TodayisMonday.");
caseWeek.Tuesday:return("TodayisTuesday.");
caseWeek.Wednesday:return("TodayisWednesday.");
caseWeek.Thursday:return("TodayisThursday.");
caseWeek.Friday:return("TodayisFriday.");
caseWeek.Saturday:return("TodayisSaturday.");
caseWeek.Sunday:return("TodayisSunday.");
default:return("nosuchday");
}
System. Enum Method
Three useful methods are enum. isdefined, enum. parse, and enum. getname.
All three methods are staticmethod. The first two methods are often used together to determine whether a value or symbol is an enumeration member and then create an instance of it.
The isdefined method has two parameters: one is the enumeration type returned by the typeof operator, and the other is the string to be tested. If a number is passed as the second parameter, this is the second form of this method, used to test whether a specified constant exists.
The parse method selects the same parameters and creates an instance of the enumeration type. Before using the parse method, make sure that the enumerated member already exists. Otherwise, the system throws an exception.
The getname method returns the corresponding string in the enumeration Based on the specified value (passed as the second parameter. For example
stringtues=Enum.GetName(typeof(Week),2); tues=Tuesday
Here is an instance used to determine whether it contains symbols matching the given string value. If yes, create an instance of this Enum and use the getname method to print a member value.
Tostring method of Enum
Here is a program I saw on csdn. Reading this program can not only understand the tostring method of Enum, but also understand the relationship between symbols and values.
using System; class Sample
{
enum Colors {Red, Green, Blue, Yellow};
public static void Main()
{
Colors myColor = Colors.Yellow;
Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));
Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));
Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));
Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d")); Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine); Console.WriteLine("myColor.ToString("g") = {0}", myColor.ToString("g"));
Console.WriteLine("myColor.ToString("G") = {0}", myColor.ToString("G")); Console.WriteLine("myColor.ToString("x") = {0}", myColor.ToString("x"));
Console.WriteLine("myColor.ToString("X") = {0}", myColor.ToString("X")); Console.WriteLine("myColor.ToString("d") = {0}", myColor.ToString("d"));
Console.WriteLine("myColor.ToString("D") = {0}", myColor.ToString("D")); Console.WriteLine("myColor.ToString("f") = {0}", myColor.ToString("f"));
Console.WriteLine("myColor.ToString("F") = {0}", myColor.ToString("F"));
}
}
/*
This example produces the following results:
Colors.Red = 0
Colors.Green = 1
Colors.Blue = 2
Colors.Yellow = 3 myColor = Colors.Yellow myColor.ToString("g") = Yellow
myColor.ToString("G") = Yellow
myColor.ToString("x") = 00000003
myColor.ToString("X") = 00000003
myColor.ToString("d") = 3
myColor.ToString("D") = 3
myColor.ToString("f") = Yellow
myColor.ToString("F") = Yellow
*/
Enum. tostring method ()
Return Value
String Representation of the value of this instance.
Remarks
This method is the same as specifying the common format character "G. That is to say, if flagsattri is not applied to this enumeration type and a named constant is equal to the value of this instance, the return value is a string containing the constant name. If flagsattribute is applied and a combination of one or more named constants equal to the value of this instance exists, the return value is a string that contains a list of constant names separated by delimiters. In other cases, the return value is a string representation of the value of this instance.
For more information about formatting characters, see the remarks section of the format method. For more information about General formatting, see formatting overview.
. NET Framework Lite version-Windows CE. NET platform Description: Because this method searches for metabases, it occupies a large amount of system resources, which may affect performance.
Example
[C #]
using System; public class EnumSample {
enum Colors {Red = 1, Blue = 2};
public static void Main() {
Enum myColors = Colors.Red;
Console.WriteLine("The value of this instance is ’{0}’",
myColors.ToString());
}
}
/*
Output.
The value of this instance is ’Red’.
*/
Enumeration and Bit Flag
We often set the enumerated value to the power of 2 because enumeration members often perform logical operations. In this case, the power of 2 is a significant advantage, that is, they can be mapped to a binary bit. The following is an example:
enumfabric
{
cotton=1,
silk=2,
wool=4,
rayon=8,
other=128,
}
fabricfab=fabric.cotton|fabric.wool;
Console.WriteLine(fab.ToString()); //output:5
If the output result can be expressed as a combination of wool and cotton, it will be more interesting. You can add the [flags] attribute to the enumeration.
[Flags]
enumfabric
{
cotton=1,
silk=2,
wool=4,
rayon=8,
other=128,
}
fabricfab=fabric.cotton|fabric.wool;
Console.WriteLine(fab.ToString("g")); //output:cotton,wool