Enums in C#
Enum:
v An
enum is a collection of named integer constants.
v To
create an enum, use the enum keyword.
v Enum
is short for "enumerations", which means "specifically
listed".
v By
default, the associated constant values of enum members are of type int and
the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
v Enums
are not for end-users, they are meant for developers.
v Enum
values are fixed. Enum can be displayed as a string and processed as an
integer.
v An
explicit cast is required to convert from enum type to an integral type.
v Enums
are value types and are created on the stack and not on the heap.
v Enum
constants has default values which starts from 0 and incremented to one by
one. But we can change the default value.
v We
can only assign integral values to the enum names. We
should not assign strings as values.
Enum in C# can be declared within or
outside class and structs.
Enum Syntax:
The syntax for an enum is given as
follows:
AccessSpecifier enum NameOfEnum
{
// The enumerator list separated by comma (,)
};
In the above syntax,
The keyword enum is
used to create an enumeration with the name NameOfEnum.
Then the enumeration list is available within
curly braces separated by comma (,) in the enum body.
The value of the first identifier in the
enumerator list is zero by default.
Advantages of Enums:
Enumerations (Enums) make your code
more readable understandable and reduce complexity.
Comments
Post a Comment