Enumeration

An enumeration is a set of discrete and related values. You would use an enumeration if you wanted to create a variable that is limited to a set of values, such as one that represents months or one that includes a list of flags. Enumerations expand into a type where the members of the enumeration are defined as constants. The default underlying type of an enumeration is integer. An enumeration is never required; an integer variable can be used directly instead. However, enumerations are safer and more extensible than integer variables, and they enhance readability. In the following sample code, the Months.GetMonth method outputs the name of a month. The method relies on integer values:

public class Months {
   static public void GetMonth(int iMonth) {
       switch(iMonth) {
           case 1:
               Console.WriteLine("January");
               break;
           case 2:
               Console.WriteLine("Febuary");
               break;
           case 3:
               Console.WriteLine("March");
               break;
           // and so on...
           default:
               Console.WriteLine("Invalid Month");
               break;
        }
    }
}

Here is the Months class rewritten using enumeration. The code is simpler and more readable:

public enum Month {
    January = 1,
    February,
    March
}
public class Months{
    public static void GetMonth(Month m) {
        Console.WriteLine(m.ToString());
    }
}

Here is the syntax of an enumeration:

  • accessibility modifiers enum identifier: basetype { memberlist };

Enumerations support all accessibility. As previously mentioned, the default underlying type is integer. The basetype element changes the underlying type. The base type can be any integral type. For the Month enumeration, changing the underlying type from integer to byte is more efficient. The memberlist includes all the constants of the enumeration. By default, the constant members are numbered in textual order from zero to (number of elements - 1). Each member can be assigned a specific value. Successive members are incremented by 1 from that value. Here is another enumeration for months:

public enum Month: byte {
    January = 1,
    February,
    March
}

When assigning values to enumeration members, sequential ordering is not required. In addition, duplicate values are allowed. Some members can be set without initializing others. ZEnum is a mixture of explicitly and implicitly initialized members. Implicit values are listed in the comments:

public enum ZEnum {
    item1 = 6,
    item2 = 3,
    item3,    // 4
    item4,    // 5
    item5 = 8,
    item6     // 9
}

Enumerations are instances of the System.Enum class, which offers important services for enumeration types. Table 2-5 lists some of the more important methods.

Table 2-5. Important System.Enum methods

Method name

Description

static string GetName(Type enumtype, object value)

Returns the string representation of a specific element in the enumeration. The ToString method also returns the string representation of the value.

static string[ ] GetNames(Type enumtype)

Returns a string array containing the string representation of every item of the specified enumeration.

static Array GetValues(Type enumtype)

Returns an array of the underlying type. The array contains the numerical representation of each value of the enumeration.

static Type GetUnderlyingType(TypeenumType)

Returns the underlying type of the enumeration.

Bitwise Enumeration

Bitwise enumeration is for mutually inclusive flags. Each member of the enumeration is assigned a unique bitwise value. Apply the flags attribute to an enumeration to specify bitwise enumeration.

Combine bitwise flags using the OR operator (|). Confirm the existence of a flag with the bitwise AND operator (&).

The following code demonstrates the [Flags] attribute and enumeration:

using System;

namespace Donis.CSharpBook {

    [Flags] public enum Contribution {
        Pension = 0x01,
        ProfitSharing = 0x02,
        CreditBureau = 0x04,
        SavingsPlan = 0x08,
        All=Pension|ProfitSharing|CreditBureau|SavingsPlan
    }

    public class Employee {
        private Contribution prop_contributions;
        public Contribution contributions {
            get {
                return prop_contributions;
            }
            set {
                prop_contributions = value;
            }
        }
    }

    public class Starter {
        public static void Main() {
            Employee bob = new Employee();
            bob.contributions = Contribution.ProfitSharing|
                Contribution.CreditBureau;
            if ((bob.contributions & Contribution.ProfitSharing)
                == Contribution.ProfitSharing) {
                Console.WriteLine("Bob enrolled in profit sharing");
            }
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.32.222