Csharp/CSharp Tutorial/Data Type/System.Enum
Содержание
- 1 Enum elements compare
- 2 Enum.IsDefined()
- 3 Get all stats for an enum: Enum.GetValues()
- 4 Get an enum"s type, hex and value: Enum.Format()
- 5 Get underlying type of an enum: Enum.GetUnderlyingType()
- 6 Loop through an enum data type
- 7 Parse a string to an enum: Enum.Parse
- 8 The System.Enum Type: enum value from a string, ignore case
- 9 The System.Enum Type: GetNames
- 10 The System.Enum Type: GetValues, GetName and GetType
- 11 The System.Enum Type: see if a specific value is a defined enum member
Enum elements compare
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { EmployeeType Joe = EmployeeType.Developer; EmployeeType Fran = EmployeeType.Programmer; if(Joe < Fran) Console.WriteLine("Joe"s value is less than Fran"s"); else Console.WriteLine("Fran"s value is less than Joe"s"); }
}</source>
Fran"s value is less than Joe"s
Enum.IsDefined()
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { if(Enum.IsDefined(typeof(EmployeeType), "SalesPerson")) Console.WriteLine("Yep, we have sales people."); else Console.WriteLine("No, we have no profits...."); }
}</source>
No, we have no profits....
Get all stats for an enum: Enum.GetValues()
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { Array obj = Enum.GetValues(typeof(EmployeeType)); Console.WriteLine("This enum has {0} members:", obj.Length); }
}</source>
This enum has 4 members:
Get an enum"s type, hex and value: Enum.Format()
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { EmployeeType fred; fred = EmployeeType.Developer; Console.WriteLine("You are a {0}", fred.ToString()); Console.WriteLine("Hex value is {0}", Enum.Format(typeof(EmployeeType), fred, "x")); Console.WriteLine("Int value is {0}", Enum.Format(typeof(EmployeeType), fred, "D")); }
}</source>
You are a Developer Hex value is 09 Int value is 9
Get underlying type of an enum: Enum.GetUnderlyingType()
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { Console.WriteLine(Enum.GetUnderlyingType(typeof(EmployeeType))); }
}</source>
System.Byte
Loop through an enum data type
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { Array obj = Enum.GetValues(typeof(EmployeeType)); foreach(EmployeeType e in obj) { Console.Write("String name: {0}", Enum.Format(typeof(EmployeeType), e, "G")); Console.Write(" ({0})", Enum.Format(typeof(EmployeeType), e, "D")); Console.Write(" hex: {0}\n", Enum.Format(typeof(EmployeeType), e, "X")); Console.WriteLine(); } }
}</source>
String name: Programmer (1) hex: 01 String name: Developer (9) hex: 09 String name: Manager (10) hex: 0A String name: Contractor (100) hex: 64
Parse a string to an enum: Enum.Parse
<source lang="csharp">using System; enum EmployeeType : byte {
Manager = 10, Programmer = 1, Contractor = 100, Developer = 9
} class MainClass {
public static void Main(string[] args) { EmployeeType sally = (EmployeeType)Enum.Parse(typeof(EmployeeType), "Manager"); Console.WriteLine("Sally is a {0}", sally.ToString()); }
}</source>
Sally is a Manager
The System.Enum Type: enum value from a string, ignore case
<source lang="csharp">using System; enum Color {
red, green, yellow
} public class MainClass {
public static void Main() { Color c = Color.red; // c = (Color) Enum.Parse(typeof(Color), "Red", true); Console.WriteLine("string value is: {0}", c); }
}</source>
string value is: red
The System.Enum Type: GetNames
<source lang="csharp">using System; enum Color {
red, green, yellow
} public class MainClass {
public static void Main() { Color c = Color.red; foreach (string s in Enum.GetNames(c.GetType())) { Console.WriteLine("Name: {0}", s); } }
}</source>
Name: red Name: green Name: yellow
The System.Enum Type: GetValues, GetName and GetType
<source lang="csharp">using System; enum Color {
red, green, yellow
} public class MainClass {
public static void Main() { Color c = Color.red; // enum values and names foreach (int i in Enum.GetValues(c.GetType())) { Console.WriteLine("Value: {0} ({1})", i, Enum.GetName(c.GetType(), i)); } }
}</source>
Value: 0 (red) Value: 1 (green) Value: 2 (yellow)
The System.Enum Type: see if a specific value is a defined enum member
<source lang="csharp">using System; enum Color {
red, green, yellow
} public class MainClass {
public static void Main() { bool defined = Enum.IsDefined(typeof(Color), 5); Console.WriteLine("5 is a defined value for Color: {0}", defined); }
}</source>
5 is a defined value for Color: False