Home > C# Code Examples > C# Enums

C# Enums


using System;

using System.ComponentModel;

using System.Diagnostics;

 

 

public class EnumFunctions

{

    public enum MyEnum : int

    {

        alfa = 1,

        beta = 2,

        gamma = 3

    }

 

    public enum ProjectTypes

    {

        Unknown = 0,

        Closing = 1,

        Generic = 2,

        Listing = 3,

        Buying = 4

    }

 

    enum Color

    {

        Green,

        Yellow,

        Red

    }

 

    public EnumFunctions()

    { 

        //How to retrieve an Enum member given its integer value

        //To retrieve an Enum member given its integer value, cast the integer value to your Enum type:

 

        MyEnum MyEnumVal;

        int i;

 

        // Retrieve an enum member by its value

        i = 2;

        MyEnumVal = (MyEnum)i;

        Debug.WriteLine(MyEnumVal.ToString());

 

        // If the specified value is not an enum member, MyEnumVal receives the value anyway

        i = 4;

        MyEnumVal = (MyEnum)i;

        Debug.WriteLine(MyEnumVal.ToString());    // The output is “4”

 

        // Test for allowed enum values

        if (!MyEnum.IsDefined(typeof(MyEnum), 4))

        Debug.WriteLine(“The value of 4 is not defined in the Enum”);

 

        //How to retrieve an Enum member given its name

        //To retrieve an Enum member given its name, use the ConvertFrom() method of the

        //TypeConverter class and cast the result to your Enum type:

 

        MyEnumVal = (MyEnum) TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom(“gamma”);

        Debug.WriteLine(MyEnumVal.ToString());    // The output is “gamma”

 

        // or

        MyEnumVal = (MyEnum) Enum.Parse(typeof(MyEnum), “gamma”);

        Debug.WriteLine(MyEnumVal.ToString());    // The output is “gamma”

 

        //Using Enums

        switch ((ProjectTypes)Convert.ToInt32(projectTypeID))

        {

            case ProjectTypes.Buying:

            break;

            case ProjectTypes.Closing:

            break;

            case ProjectTypes.Generic:

            break;

            case ProjectTypes.Listing:

            break;

            case ProjectTypes.Unknown:

            break;

            default:

            break;

        }

 

        // or

 

        ProjectTypes projectType = (ProjectTypes)Enum.Parse(typeof(ProjectTypes), projectTypeID)

 

        // or cast from int to enum like

        ProjectTypes pType = (ProjectTypes ) intStatus;

        switch (pType)

        {

            case ProjectTypes.Buying:

            fileType = “Buying”;

            break;

 

            case ProjectTypes.Closing:

            fileType = “Closing”;

            break;

            //…

            default:

            break;

        }

 

        //C# / CSharp Tutorial » Data Type » enum base type     

        //Convert enum to int

 

        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);

    }

}

Advertisements
Categories: C# Code Examples
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: