Enumerations Fundamentals

Introduction

One of the most regular operations you perform in your applications is that of comparisons. You must compare two numbers, two strings, etc. When it comoes to numbers, instead of using vague values, you can create a list of numbers that have meaning names

An enumeration is a list of numbers that each represents a specific constant value.

Creating an Enumeration

To create an enumeration, you can manually type the code. To use a code snippet, right-click where you want to add the enumeration and click Insert Snippet... Double-click Visual C#. In the list, double-click enum:

Enum

The formula to create an enumeration is:

access-level enum enumeration-name {item1, item2, item_n};

Start with the enum keyword followed by a name. The name of an enumeration follows the rules and suggestions of names of classes. The name of followed by a body delimited by { and }. In that body, create a list of names separated by commas. Each name is a member, element or item of the enumeration. Each name must be in one word. It too follows the rules and suggestions of names of classes. Here is an example:

public class Exercise
{
    enum HouseType { Single, Married }
    
    static int Main()
    {
        return 0;
    }
}

To make the code easy to read, especially if the enumeration contains many members, you can put each curcly bracket on its own line, you can put all members on the same line, some members on same lines, or each member on its own line. Here is an example:

public class PoliticalParty
{
    enum Alignment
    {
        FarLeft,
        Left,
        CenterLeft,
        Center,
        CenterRight,
        Right,
        FarRight
    }
}

Enumerations Visibility

By default, if you create an enumeration the way we have proceeded so far, it would be available only in the project to which it belongs. As done for a class, you can control an enumeration's accessibility outside of its project. This means that you can hide or make it visible outside of its project. To do this, you can precede it with the private or the public keyword. Here is an example:

using static System.Console;

public class Exercise
{
    public enum HouseType
    {
        Unknown,
        SingleFamily,
        TownHouse,
        Condominium
    }
    
    static int Main()
    {
        HouseType propType = HouseType.SingleFamily;

        WriteLine("House Type:  {0}", propType);
        return 0;
    }
}

A Variable of Enumeration Type

After creating an enumeration, you can declare a variable from it. An enumeration can be created inside (in the body of) a class or outside. If you are planning to use an enumeration from only one class, create it inside that class without specifying its access level or by applying private. Such an enumeration is considered private.

To declare a variable of an enumeration, use its name followed by a name for the variable. Here is an example:

public class RealEstate
{
    enum PropertyType { Unknown, SingleFamily, TownHouse, Condominium }

    public RealEstate()
    {
        
    }

    PropertyType type;
}

If you try using a private enumeration outside its class, you would receive an error. If you are planning to use an enumeration from more than one class, you can create it inside or outside a class. If you want to access an enumeration from more than one class, you can create it outside the class. Here is an example:

enum PropertyType { Unknown, SingleFamily, TownHouse, Condominium }

public class RealEstate
{
    PropertyType type;
}

public class Exercise
{
    PropertyType category;
}

In this case, you can optionally mark the enumeration as public or internal. You can also create the enumeration inside a class. In this case, you must apply the public or internal access level. To access such an enumeration, you must qualify it from the name of its class. Here are examples:

public class Management
{
    public enum PropertyType { Unknown, SingleFamily, TownHouse, Condominium }
}

public class RealEstate
{
    Management.PropertyType type;
}

public class Exercise
{
    Management.PropertyType category;
}

If the enumeration belongs to a namespace other than the one where it is used, remember to qualify its name. Here is an example:

namespace PresidentialElections
{
    enum Alignment
    {
        FarLeft,
        Left,
        CenterLeft,
        Center,
        CenterRight,
        Right,
        FarRight
    }
}

class Exercise
{
    static void Main()
    {
        PresidentialElections.Alignment leaning;
    }
}

As is the case for every type, you can also use the var keyword to declare a variable of an enumeration type.

Accessing a Member of an Enumeration

An enumeration is (only) useful for its members, which you access when necessary. To access a member of an enumeration, type the name of the enumeration, followed by the period operator, and followed by the member you want to access.

Initializing an Enumeration Variable

When declaring a variable for an enumeration, if you want to initialize it, access the member you want and assign it to the variable. You can only assign a member of the enumeration. Here is an example:

using static System.Console;

public class Exercise
{
    private enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        var propType = HouseType.SingleFamily;

        return 0;
    }
}

Remember that you can get assistance from the Intellisence. When you type the name of an enumeration and a period, the Intellisence would display the list of the members of the enumeration.

Displaying the Value of an Enumeration Variable

To display the value of an enumeration, pass its variable to the Console.Write() or the Console.WriteLine() method. Here is an example:

using static System.Console;

public class Exercise
{
    enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
    
    static int Main()
    {
        var propType = HouseType.SingleFamily;

        WriteLine("House Type:  {0}", propType);

        return 0;
    }
}

This would produce:

House Type:  SingleFamily
Press any key to continue . . .

Enumerations and Classes

An Field of an Enumeration Type

Once an enumeration exists, it becomes a type like any other. For example, you can create a field of an enumeration in a class. To create a field that is of an enumeration type, follow the same rules as done for the primitive types: the name of the enumeration, followed by the name of the variable, and followed by a semi-colon. Here is an example:

public enum HouseType
{
    Unknown,
    SingleFamily,
    TownHouse,
    Condominium
}

public class House
{
    HouseType propertyType;
}

A Method that Returns an Enumeration

You can create a method that returns a member of an enumeration. When creating the method, type the name of the enumeration to the left of the name of the method. In the body of the method, do what you want. Before the closing curly bracket, return a member qualified from the name of the enumeration. Here is an example:

public class PoliticalParty
{
    private enum Alignment
    {
        FarLeft,
        Left,
        CenterLeft,
        Center,
        CenterRight,
        Right,
        FarRight
    }

    Alignment IndicateAbortionPrinciple()
    {
        Alignment choice = Alignment.CenterLeft;

        return choice;
    }
}

If you create a private enumeration (an enumeration created in the body of a class, as private or without an access level), only private methods that returns it can access that enumeration. A public method that returns an enumeration cannot access a private enumeration. As a result, the following code produces an error:

public class PoliticalParty
{
    private enum Alignment
    {
        FarLeft,
        Left,
        CenterLeft,
        Center,
        CenterRight,
        Right,
        FarRight
    }

    public Alignment IndicateAbortionPrinciple()
    {
        Alignment choice = Alignment.CenterLeft;

        return choice;
    }
}

Therefore, if you decide to create an enumeration inside a class, it may be a good idea to make it public because when a private method returns a public enumeration, there is no problem.

An Enumeration as Parameter

An enumeration, as a type, can be passed as argument. To create a parameter that is an enumeration type, precede the enumeration with the name of the parameter. Here is an example:

public class Exercise
{
    private static void ShowHouse(HouseType propType)
    {

    }
}

In the same way, you can pass as many enumeration types as necessary. In the body of the procedure, you can use the enumeration or ignore it. When calling the procedure, pass an argument that holds a value of the type of enumeration. Here is an example:

using static System.Console;

public enum HouseType
{
    Unknown = 2,
    SingleFamily = 4,

    TownHouse = 6,
    Condominium = 8,
}

public class Exercise
{
    private static void ShowHouse(HouseType propType)
    {
        WriteLine("Type of house: {0}", propType);
    }

    public static int Main()
    {
        HouseType ht;

        ht = HouseType.SingleFamily;
        ShowHouse(ht);

        ReadKey();
        return 0;
    }
}

This would produce:

Type of house: SingleFamily
Press any key to continue . . .

You can also pass the argument as optional. When creating the method, use the assignment operator to specify that the argument has a default value and assign the desired member of the enumeration. When calling the method, you can pass, or omit passing, a value for the argument. Here is an example:

using static System.Console;

public enum HouseType
{
    Unknown = 2,
    SingleFamily = 4,
    TownHouse = 6,
    Condominium = 8,
}

public class Exercise
{
    private static void ShowHouse(HouseType propType = HouseType.Unknown)
    {
        WriteLine("Type of house: {0}", propType);
    }

    public static int Main()
    {
        HouseType ht;

        ht = HouseType.SingleFamily;
        ShowHouse();
        return 0;
    }
}

This would produce:

Type of house: Unknown
Press any key to continue . . .

Previous Copyright © 2001-2019, FunctionX Next