Enumerations
Posted by Gizmosis350k
Last Updated: April 21, 2012
  2072

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch05Ex02
{
   enum orientation : byte
   {
      north = 1,
      south = 2,
      east = 3,
      west = 4
   }

   class Program
   {
      static void Main(string[] args)
      {
         byte directionByte;
         string directionString;
         orientation 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);

         Console.ReadKey();
      }
   }
}


This code is supposed to show the usefulness of enumerations.
Basically, from what i understand, these are variables that can map values to integers, starting with 0, or whichever integer you want assigned to them. That way, a enumeration can have a "short" and a "string" representation of the same value, or something to that effect, i will be rereading through this section in the enar future.
   
  
 
 
   

 
 

The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:

      enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. Enumerators can have initializers to override the default values. For example:

 enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

 
giz u a shot! Soon join u :)
 
Yea so i noticed this code went on to show the numeric and string representation for a entry in the enumeration (or elements) so to speak, could you give me another quick example of the practical use of this complex variable type?
 
 E.G (1)
using System;

class Program
{
    static void Main()
    {
	// A.
	// Two enum variables:
	B b1 = B.Dog;
	V v1 = V.Hidden;

	// B.
	// Print out their values:
	Console.WriteLine(b1);
	Console.WriteLine(v1);
    }

    enum V
    {
	None,
	Hidden = 2,
	Visible = 4
    };

    enum B
    {
	None,
	Cat = 1,
	Dog = 2
    };
}

E.G (2)
using System; class Program { enum E { None, BoldTag, ItalicsTag, HyperlinkTag, }; static void Main() { // A. // These values are enum E types. E en1 = E.BoldTag; E en2 = E.ItalicsTag; if (en1 == E.BoldTag) { // Will be printed. Console.WriteLine("Bold"); } if (en1 == E.HyperlinkTag) { // Won't be printed. Console.WriteLine("Not true"); } } }