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.
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);
public enum DayOfWeek
{
[Description("Sunday")]
[ResourceName("DAY_OF_WEEK_SUNDAY")]
Sunday = 1,
[Description("Monday")]
[ResourceName("DAY_OF_WEEK_MONDAY")]
Monday = 2,
[Description("Tuesday")]
[ResourceName("DAY_OF_WEEK_TUESDAY")]
Tuesday = 3,
[Description("Wednesday")]
[ResourceName("DAY_OF_WEEK_WEDNESDAY")]
Wednesday = 4,
[Description("Thursday")]
[ResourceName("DAY_OF_WEEK_THURSDAY")]
Thursday = 5,
[Description("Friday")]
[ResourceName("DAY_OF_WEEK_FRIDAY")]
Friday = 6,
[Description("Saturday")]
[ResourceName("DAY_OF_WEEK_SATURDAY")]
Saturday = 7,
}
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. |
Sources:
Comments