Csharp/CSharp Tutorial/Data Type/enums Definition

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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>