blog

Home / DeveloperSection / Blogs / Enum Class in C#

Enum Class in C#

Vijay Shukla3847 17-Jun-2013

In this blog I am trying to explain the concept of enum Class in C#.

Enum Class

An enumeration is a set of named constants whose underlying type is any integral type. If no underlying type is explicitly declared, Int32 is used. Enum is the base class for all enumerations in the .NET Framework. Enum provides methods for comparing instances of this class, converting the value of an instance to its string representation, converting the string representation of a number to an instance of this class, and creating an instance of a specified enumeration and value.

Example: -
using System;
public enum ArrivalStatus { Unknown = -3, Late = -1, OnTime = 0, Early = 1 };
public class EnumSample
{
    public static void Main()
    {
        int[] values = { -3, -1, 0, 1, 5, Int32.MaxValue };
        foreach (var value in values)
        {
            ArrivalStatus status;
            if (Enum.IsDefined(typeof(ArrivalStatus), value))
                status = (ArrivalStatus)value;
            else
                status = ArrivalStatus.Unknown;
            Console.WriteLine("Converted {0:N0} to {1}", value, status);
            Console.ReadKey();
        }
    }
}
Output: -

Enum Class in C#

Updated 18-Sep-2014

Leave Comment

Comments

Liked By