While I code every time I used List<T>
, string
, bool
etc. I did't see anywhere a use of an enum.
I have an idea that enum
is a constant but in practice, where do we actually use it. If at all we can just use a
public const int x=10;
Where do we actually use it?
Kindly help me
A constant lets us define a name for a value in one place in our code.
An enum is like defining a set of constants and lets us declare variables, properties, and parameters that can only use one of those constants.
For example, suppose we have a
SalesOrder
class for orders we receive on a website, and eachSalesOrder
can have a status - perhaps New, Shipped, Canceled, etc.We could do it like this:
But then someone could set that property to something completely invalid, like
We could decide that we'll give each status a number instead to prevent someone using some crazy value. So we change it to
and we decide that 1 = New, 2 = Shipped, 3 = Canceled, etc. But that still doesn't fix anything, because someone can set
OrderStatusCode = -666
and we're still messed up.In any one of these cases we could improve on this with constants, like
or
But that still doesn't really solve the problem. It helps us to do this:
and that's good. But it still doesn't prevent this:
An
enum
lets us do this:Now it's impossible to set
OrderStatus
to any invalid value. It can only be one of the values inOrderStatuses
.Comparisons become a lot easier too. Instead of
or
We can do
Now it's readable and easier to maintain. The compiler will prevent using any invalid value. If you decide you want to change the name of a value in
OrderStatuses
you can just right-click and rename it. You can't do that with astring
or anint
.So an
enum
is very useful in that scenario - if we want to have a type with a limited, predefined set of values.The most common use for constants is if we're putting a string or a number in our code that either repeats or has no apparent meaning, like
We might declare a constant like
Now this makes sense:
It's a small detail but it signals our intent and keeps us from storing a value in multiple places.
An
enum
is a convenient way to use names instead of numbers, in order to denote something. It makes your code far more readable and maintainable than using numbers. For instance, let that we say that 1 is red and 2 is green. What is more readable the following:or this:
Furthermore, let that you make the above checks in twenty places in your code base and that you have to change the value of Red from 1 to 3 and of Green from 2 to 5 for some reason. If you had followed the first approach, then you would have to change 1 to 3 and 2 to 5 in twenty places ! While if you had followed the second approach the following would have been sufficient:
Suppose you need to flag users in a software with roles, then you can declare an enum to define these roles, for sample:
Then, you can use this type
UserRole
in yourUser
entity. It work as an integer value but it is more legible than an integer.You could implement something like this:
or
Just adding a little bit more on enums: Enumerators are named constants. If you are using a set of named constants in your application you can go with enums instead of hard coding those constants. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. But you can change the default type.
By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. You can change the first value
The sequence of elements is forced to start from 1 instead of 0.
You can change the default type of enum, But must be any integer type.
Practical Scenario You can create a enum of status of your project Task
And you can use the enum like