Enum in C#:
In C#, enum is a value type data type. The enum is used to declare a list of named integer constants. It can be defined using the enum keyword directly inside a namespace, class, or structure. The enum is used to give a name to each constant so that the constant integer can be referred using its name.
Example: enum
enum WeekDays
{
Monday = 0,
Tuesday =1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday =5,
Sunday = 6
}
Console.WriteLine(WeekDays.Friday);
Console.WriteLine((int)WeekDays.Friday);
Output:
Friday
4
By default, the first member of an enum has the value 0 and the value of each successive enum member is increased by 1. For example, in the following enumeration, Monday is 0, Tuesday is 1, Wednesday is 2 and so forth.
Example: enum
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Console.WriteLine((int)WeekDays.Monday);
Console.WriteLine((int)WeekDays.Friday);
Output:
0
4
An explicit cast is necessary to convert from enum type to an integral type. For example, to get the int value from an enum:
Example: enum
int dayNum = (int)WeekDays.Friday;
Console.WriteLine(dayNum);
Output:
4
A change in the value of the first enum member will automatically assign incremental values to the other members sequentially. For example, changing the value of Monday to 10, will assign 11 to Tuesday, 12 to Wednesday, and so on:
Example: enum
enum WeekDays
{
Monday = 10,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Console.WriteLine((int)WeekDays.Monday);
Console.WriteLine((int)WeekDays.Friday);
Output:
10
14
The enum can includes named constants of numeric data type e.g. byte, sbyte, short, ushort, int, uint, long, or ulong.
enum cannot be used with string type.
Enum is mainly used to make code more readable by giving related constants a meaningful name. It also improves maintainability.
Enum methods:
Enum is an abstract class that includes static helper methods to work with enums.
Enum method | Description |
---|
Format | Converts the specified value of enum type to the specified string format. |
GetName | Returns the name of the constant of the specified value of specified enum. |
GetNames | Returns an array of string name of all the constant of specified enum. |
GetValues | Returns an array of the values of all the constants of specified enum. |
object Parse(type, string) | Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. |
bool TryParse(string, out TEnum) | Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded. |
Example: enum mehtods
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));
Console.WriteLine("WeekDays constant names:");
foreach (string str in Enum.GetNames(typeof(WeekDays)))
Console.WriteLine(str);
Console.WriteLine("Enum.TryParse():");
WeekDays wdEnum;
Enum.TryParse<WeekDays>("1", out wdEnum);
Console.WriteLine(wdEnum);
Output:
Friday
WeekDays constant names:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Enum.TryParse():
Tuesday
Points to Remember :
- The enum is a set of named constant.
- The value of enum constants starts from 0. Enum can have value of any valid numeric type.
- String enum is not supported in C#.
- Use of enum makes code more readable and manageable.