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();
}
}
}
Anonymous User
23-Jul-2019Thanks for the informative post.