Saturday, May 5, 2018

C#: What is enum in C#

An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.

An enum type can be an integer (float, int, byte, double etc.). But if you used beside int it has to be cast.

An enum is used to create numeric constants in .NET framework. All the members of enum are of enum type. Their must be a numeric value for each enum type.

The default underlying type of the enumeration element is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
  1. enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};  
Some points about enum
  • Enums are enumerated data type in c#.
  • Enums are not for end-user, they are meant for developers.
  • Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • Enum values are fixed. Enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.
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:

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 methodDescription
FormatConverts the specified value of enum type to the specified string format.
GetNameReturns the name of the constant of the specified value of specified enum.
GetNamesReturns an array of string name of all the constant of specified enum.
GetValuesReturns 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
Visit MSDN to know more about enum methods.

Points to Remember :

  1. The enum is a set of named constant.
  2. The value of enum constants starts from 0. Enum can have value of any valid numeric type.
  3. String enum is not supported in C#.
  4. Use of enum makes code more readable and manageable.

No comments:

Post a Comment