Csharp/CSharp Tutorial/Data Type/enums Definition — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
Comparing Enumeration Values with CompareTo()
<source lang="csharp">using System;
public class MainClass {
public enum Color { Red = 0, Orange, Yellow, Green, Blue, Indigo, Violet } static void Main() { Color MyColor; MyColor = Color.Green; Console.WriteLine("{0}", MyColor.rupareTo(Color.Red)); Console.WriteLine("{0}", MyColor.rupareTo(Color.Green)); Console.WriteLine("{0}", MyColor.rupareTo(Color.Violet)); }
}</source>
Define Enums
<source lang="csharp">enum Colors {
red, green, blue
}</source>
Retrieving an Enumeration Name with GetName()
<source lang="csharp">using System;
public enum LegalDoorStates {
DoorStateOpen, DoorStateClosed
}
class DoorController {
private LegalDoorStates CurrentState; public LegalDoorStates State { get { return CurrentState; } set { CurrentState = value; } }
}
class MainClass {
public static void Main() { DoorController Door; string EnumName; Door = new DoorController(); Door.State = LegalDoorStates.DoorStateOpen; EnumName = LegalDoorStates.GetName(typeof(LegalDoorStates), Door.State); Console.WriteLine(EnumName); }
}</source>
Using the LegalDoorStates Enumeration
<source lang="csharp">public enum LegalDoorStates {
DoorStateOpen, DoorStateClosed
}
class DoorController {
private LegalDoorStates CurrentState; public LegalDoorStates State { get { return CurrentState; } set { CurrentState = value; } }
}
class MainClass {
public static void Main() { DoorController Door; Door = new DoorController(); Door.State = LegalDoorStates.DoorStateOpen; }
}</source>