Enumerations in C#

Enumerations are a special kind of data type. Enumerations contain a list of named constants.
They can make code more readable, as they allow you to reference constants by using a name instead of the literal value.
In C#, enumerations are declared by using the
enum keyword.
Enumerations can be declared as
byte, sbyte, short, ushort, int, uint, long and ulong.
By default, enumerations are defined as
int.


enum animal
{
Dog,
Cat,
Fish,
Bird
}



A comma must separate each member in the enumeration.
If values for the enumeration members are not explicitly defined, the first member defaults to 0.
In the
Animal example, the Dog member equates to 0; the Cat equates to 1, and so on.
Members can be initialized to constant values if needed.




enum Animal
{
Dog = 3,
Cat,
Fish,
Bird
}


Here, members are initialized starting at 3.
Cat equals 4 and so on.
In reality each member can be initialized to any constant expression.
The two preceding examples do not define an underlying type for the enumeration.
Animal, in this case, defaults to an integer enumeration. Animal can be defined as a long , as in the following example.


enum Animal : long
{
Dog = 3,
Cat,
Fish,
Bird
}


Here I explained basics of enumerations.
For more information please go through the following links.
http://www.codeproject.com/KB/cs/LocalizingEnums.aspx
http://www.softsteel.co.uk/tutorials/cSharp/lesson7.html


Himasagar Kutikuppala


Comments

Author: Roopesh Babu Valluru15 Jul 2008 Member Level: Gold   Points : 0

thx for ur article..

Author: sruthi15 Jul 2008 Member Level: Bronze   Points : 0

nice article

Author: Ashish Shah17 Jul 2008 Member Level: Gold   Points : 1

Hello,
Nice article you have written for most important topic in .net..
i like this much as simple also..

With Regards..
Ashish Shah
India



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: