An enum provides a base class for enumerations whose underlying type can be any integral type other than Char. If the underlying type is not explicitly declared, the Int32 is used. Programming languages often provide syntax to declare enumerations consisting of a set of named constants and their values.
Note: The base type of an enumeration type is any integral type other than Char, so the value of the enumeration type is an integer value.
The Enum provides some useful static methods:
(1) Methods to compare instances of enumerated classes
(2) method of converting the value of an instance to its string representation
(3) method of converting a string representation of a number to an instance of this class
(4) Creates a method that specifies an instance of an enumeration and a value.
For example: Enum Colors {Red, Green, Blue, yellow};
enum-->string
(1) using Object.ToString () method: If the value of Colors.Green.ToString () is "Green" string;
(2) using the static method of enum GetName and GetNames:
public static string GetName (Type enumtype,object value)
public static string[] GetNames (Type enumtype)
For example: The values of Enum.getname (typeof (Colors), 3) and Enum.getname (typeof (Colors), colors.blue) are all "Blue"
Enum.getnames (typeof (Colors)) returns an array of enumerated strings.
String-->enum
(1) using the static method of Enum parse:
public static Object Parse (Type enumtype,string value)
For example: (Colors) Enum.parse (typeof (Colors), "Red")
Enum-->int
(1) Because the base type of the enumeration is an integral type other than Char, it can be cast.
For example: (int) colors.red, (byte) colors.green
Int-->enum
(1) You can cast an integral type to an enumeration type.
For example: Colors color = (Colors) 2, then color is Colors.blue
(2) using the static method of enum Toobject.
public static Object Toobject (Type enumtype,int value)
For example: Colors color = (Colors) enum.toobject (typeof (Colors), 2), then color is Colors.blue
A method that determines whether an integer is defined in an enumeration: Enum.isdefined
public static bool IsDefined (Type enumtype,object value)
For example: enum.isdefined (typeof (Colors), N))
public enum Employeetype
{
Regularemployee,
Storemanager,
Chainstoremanager,
Departmentmanager,
Supervisor
}
We can simply get the following information through the ToString () method
Employeetype employee = Employeetype.chainstoremanager;
Console.WriteLine (employee. ToString ());
Console.WriteLine (EmployeeType.ChainStoreManager.ToString ());
/*
Output results:
Chainstoremanager
Chainstoremanager
*/
But how do we get the results like "Chain Store Manager" including spaces? We can't create an enumeration member that contains spaces, or your code will not compile, in fact we have a lot of solutions to solve this problem.
1, create a mapping between the enumeration member and the string (you can use an array or hash table)
2, assign the result of the enumeration member ToString () as the key to the resource file
3, use reflection .... (I'll talk About)
using attributes in enumerations
I will use attributes to reach the purpose of a string association to an enumeration member, and here is a simple example to illustrate how this is done. public class enumdescriptionattribute : attribute
{
private string m_strDescription;
public enumdescriptionattribute (string strprintername)
{
m_strdescription = strprintername;
   &NBSP}
public string description
{
get { return m_strDescription; &NBSP}
}
The
Enumdescriptionattribute class inherits from the attribute, which contains a property description of type string, which is associated with all members of the enumeration Employeetype. public enum employeetype
{
[enumdescription ("Regular employee")]
regularemploye,
[enumdescription ("Store Manager")]
storemanager,
[enumdescription ("Chain Store Manager ")]
chainstoremanager,
[enumdescription (" Department manager ")]
departmentmanager,
[ Enumdescription ("On floor supervisor")]
supervisor
}
Gets the value of the property from the enumeration
in order to get the value of the property, I must use the reflection, which is a simple example:// setup the enum
employeetype employee = employeetype.chainstoremanager;
// get the field informaiton
Fieldinfo fieldinfo = employee. GetType (). GetField ("Chainstoremanager");
;