Csharp/CSharp Tutorial/Data Type/enum base type

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

Assign int value to all elements in an enum

<source lang="csharp">enum PlanetPeriods {

 Mercury = 8,
 Venus = 2,
 Earth = 3,
 Mars = 6

} class MainClass {

 public static void Main()
 {
   System.Console.WriteLine("Orbital period for Mars = " + (int) PlanetPeriods.Mars + " days");
 }

}</source>

Orbital period for Mars = 6 days

Assign int value to the first element in an enum

<source lang="csharp">enum Planets {

 Mercury = 1,
 Venus,
 Earth,
 Mars,
 Jupiter,
 Saturn,
 Uranus,
 Neptune,
 Pluto

} class MainClass {

 public static void Main()
 {
   System.Console.WriteLine("Position of Earth = " +(int) Planets.Earth);
   System.Console.WriteLine("Planets.Earth = " +Planets.Earth);
 }

}</source>

Position of Earth = 3
Planets.Earth = Earth

Bit Flag Enums

<source lang="csharp">using System; [Flags] enum BitValues: uint {

   NoBits = 0,
   Bit1 = 0x00000001,
   Bit2 = 0x00000002,
   Bit3 = 0x00000004,
   Bit4 = 0x00000008,
   Bit5 = 0x00000010,
   AllBits = 0xFFFFFFFF

} class MainClass {

   public static void Member(BitValues value)
   {
       Console.WriteLine(value);
   }
   public static void Main()
   {
       Member(BitValues.Bit1 | BitValues.Bit2);
   }

}</source>

Bit1, Bit2

Convert enum to int

<source lang="csharp">using System; enum Color {

  Green,
  Yellow,
  Red

} class MainClass {

  static void Main()
  {
     Color t1 = Color.Green;
     Color t2 = Color.Yellow;
     Color t3 = Color.Red;
     Console.WriteLine("{0},\t{1}",   t1, (int)t1);
     Console.WriteLine("{0},\t{1}",   t2, (int)t2);
     Console.WriteLine("{0},\t{1}\n", t3, (int)t3);
  }

}</source>

Green,  0
Yellow, 1
Red,    2

Define enumeration base types

<source lang="csharp">enum SmallEnum : byte {

   A,
   B,
   C,
   D

}</source>

enum based on byte

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;

   enum orientation : byte
   {
       north = 1,
       south = 2,
       east = 3,
       west = 4
   }
   class Program
   {
       static void Main(string[] args)
       {
           orientation myDirection = orientation.north;
           Console.WriteLine("myDirection = {0}", myDirection);
       }
   }</source>

enum based on byte, and convert enum variable back to byte

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;

   enum orientation : byte
   {
       north = 1,
       south = 2,
       east = 3,
       west = 4
   }
   class Program
   {
       static void Main(string[] args)
       {
           orientation myDirection = orientation.north;            
           byte directionByte;
           string directionString;
           myDirection = orientation.north;
           Console.WriteLine("myDirection = {0}", myDirection);
           directionByte = (byte)myDirection;
           directionString = Convert.ToString(myDirection);
           Console.WriteLine("byte equivalent = {0}", directionByte);
           Console.WriteLine("string equivalent = {0}", directionString);
       }
   }</source>

Print out string version of an enum value

<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(fred.ToString());
 }

}</source>

Developer

Using a base type of long when defining an enum

<source lang="csharp">enum PlanetPeriods :long {

 Mercury = 8,
 Venus = 2,
 Earth = 3,
 Mars = 6

} class MainClass {

 public static void Main()
 {
   System.Console.WriteLine("Orbital period for Mars = " + (long) PlanetPeriods.Mars + " days");
 }

}</source>

Orbital period for Mars = 6 days

Using Bit flags when declaring the enum

<source lang="csharp">using System; [Flags] enum Color : uint {

  Red = 0x01, // Bit 0
  Blue = 0x02, // Bit 1
  Yellow = 0x04, // Bit 2
  Green = 0x08 // Bit 3

} class MainClass {

  static void Main()
  {
     Color ops = Color.Red | Color.Yellow | Color.Green;
     
     bool UseRed = false, UseBlue   = false, UseYellow  = false, UseGreen = false;
     UseRed = (ops & Color.Red) == Color.Red;
     UseBlue    = (ops & Color.Blue) == Color.Blue;
     UseYellow  = (ops & Color.Yellow) == Color.Yellow;
     UseGreen  = (ops & Color.Green) == Color.Green;
     Console.WriteLine("Option settings:");
     Console.WriteLine("   Use Red    - {0}", UseRed);
     Console.WriteLine("   Use Blue   - {0}", UseBlue);
     Console.WriteLine("   Use Yellow - {0}", UseYellow);
     Console.WriteLine("   Use Green  - {0}", UseGreen);
  }

}</source>

Option settings:
   Use Red    - True
   Use Blue   - False
   Use Yellow - True
   Use Green  - True

Using Operators with Enumerations

<source lang="csharp">using System;

public enum FileAttributes {

   AttrNone = 0,
   AttrReadOnly = 1,
   AttrHidden = 2,
   AttrReadyForArchive = 4

}

class MainClass {

   public static void Main()
   {
       FileAttributes FileAttr;
  
       FileAttr = FileAttributes.AttrReadOnly | FileAttributes.AttrHidden;
       Console.WriteLine(FileAttr);
   }

}</source>