Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.43/5 (6 votes)
See more:
hi dudes...
what is the use of enum and how to use?

thanks
Posted

Enums are useful, for example, to avoid "magic" numbers - these are numbers that might be meaningful to the developer at the time they write the code, but may not be so obvious later, or to someone else.

For example days of the week can be expressed as 1,2,...,7 .. but does 1 = Monday or Sunday?
An enum defined as enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; takes away that ambiguity.

Here are some references to get you going
msdn[^]

http://www.dotnetperls.com/enum[^]

http://www.dreamincode.net/forums/topic/38937-working-with-enums-in-c%23/[^]

Google has a lot more
 
Share this answer
 
v2
Comments
ridoy 9-Oct-13 12:25pm    
5ed!
CHill60 10-Oct-13 8:49am    
Thank you! About to do the same on your solution - nice explanation on the strogly typed aspect
Enums are strongly typed constants, meaning that 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. They are essentially unique types that allow you to assign symbolic names to integral values.

Well,the best usage would be, constants can be referred to in a consistent, expressive and type safe way via Enums. See a live example why we need Enums from here:
What is main use of Enumeration?[^]

Check this CP article over Enums:
Enums in C#[^]

And complete also these 2 articles which will clear you most:
C# Station Tutorial (Lesson 17): Enums[^]
Working with enums in C# [^]
 
Share this answer
 
v3
Comments
Sushil Mate 9-Oct-13 12:58pm    
+5
ridoy 9-Oct-13 13:11pm    
Thank you.
If you have a number of constants that are logically related to each other, then you can group together these constants in an enumeration.

Enum declaration

C#
enum Tempurature
{
    Low,
    Medium,
    High,
};


Enums are strongly typed constants which makes the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged. The main advantage of Enum is make it easy to change values in the future, also you can reduces errors caused by transposing or mistyping numbers.

More about ...C# Enum

Vyar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900