// Prints out "emp is a Contractor".
Console.WriteLine("emp is a {0}.", emp.ToString());
Console.ReadLine();
}
If you are interested in discovering the value of a given enumeration variable, rather than its
name, you can simply cast the enum variable against the underlying storage type. For example:
static void Main(string[] args)
{
Console.WriteLine("**** Fun with Enums *****");
EmpType emp = EmpType.Contractor;
// Prints out "Contractor = 100".
Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);
Console.ReadLine();
}
■
Note
The static
Enum.Format()
method provides a finer level of formatting options by specifying a desired
format flag. Consult the .NET Framework 3.5 SDK documentation for full details of the
System.Enum.Format()
method.
System.Enum also defines another static method named GetValues(). This method returns an
instance of System.Array. Each item in the array corresponds to a member of the specified enumer-
ation. Consider the following method, which will print out each name/value pair within any
enumeration you pass in as a parameter:
Do'stlaringiz bilan baham: |