Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Suppose we have this enum (in c#)
C#
enum Weekdays
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }

And we need to do the following:
C#
Console.WriteLine(Weekdays.Sunday.ToString());


Here are I get output as "Sunday"

But now if I am modifying my enum as
C#
enum Weekdays
    {
        Sunday,
        Monday=Sunday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }


I get the output Monday.

Again I change my enum as:
C#
{
    Sunday,
    Monday=Sunday,
    Tuesday,
    Wednesday=Sunday,
    Thursday,
    Friday,
    Saturday
}

I get different outputs on this again. Can any one tell me what is happening here.
Posted
Updated 29-Aug-14 2:14am
v2
Comments
ZurdoDev 29-Aug-14 8:24am    
1. I didn't think you could do that because now you have 2 items in enum that have the same value.
2. What would be the purpose of ever doing this?
Richard Deeming 29-Aug-14 8:35am    
You can do it, and even some BCL enums do it. The only real purpose is to provide an alias for an enum value. For example, if you want to rename a value, but not break compilation for code using the old name.
ZurdoDev 29-Aug-14 8:40am    
That makes sense.

If you have two items that have the same value in an enum, it can;t tell which one you mean.
Bear in mind that an enum is stored as an int, and the "names" are a syntactic sugar to make your code easier to read. The names do not "carry forward" with the values, if you say:
C#
Weekdays dayOne = Weekdays.Sunday;
Weekdays dayTwo = Weekdays.Monday;
then both dayOne and dayTwo contain the same value: 0

You can't work out from a value of zero if that was Sunday, Monday or Wednesday!

Think about it: if I have a method which takes an integer:
C#
void MyMethod(int i)
   {
   Console.WriteLine(i);
   }
Can it tell the difference between these two:
C#
MyMethod(55 - 44);
and
C#
MyMethod(11);


Of course it can't - it just gets a numeric value. And that's just what a enum is!
 
Share this answer
 
v2
Comments
Nathan Minier 29-Aug-14 8:55am    
I was going to respond, fun for SNGs decided to play with it a little first. That little assignment actually resets the assignment counter for the entire enum, so it's a pointer instead of just an index! I was surprised to see this in C#, TBH.

enum weekdays
{
Sunday, // =0
Monday=Sunday, // =0
Tuesday, // =1
Wednesday=Sunday, // =0
Thursday, // =1
Friday, // =2
Saturday // =3
}

enum weekdays
{
Sunday, // =0
Monday, // =1
Tuesday, // =2
Wednesday=Sunday, // =0
Thursday, // =1
Friday, // =2
Saturday // =3
}
OriginalGriff 29-Aug-14 9:23am    
Yes - that is exactly what I would expect.
When you specify a value for an enum element, you are setting a *start value* for it and all subsequent values. That's by design:
http://msdn.microsoft.com/en-gb/library/sbbt4032.aspx
So that if you want to start them from 10 you can:
enum xx
{
start = 10,
next, // === 11
}
Anything else would be confusing! :laugh:
Hi,

I found the cause.

Its very funny...

In your case

C#
public enum Weekdays
{
        Sunday,
        Monday=Sunday,
        Tuesday,
        Wednesday=Sunday,
        Thursday,
        Friday,
        Saturday
 }


Few rule :
1. If you don't set any value to enum it will set incrementally by order of it.(0...)
2. Enum is a value type
3. Microsoft's recommendation always set a value to enum don't place it without a value.

We will discusses about few things which may irrelevant to real world but those very important to know for good developers.

You set 3 enum with same value and you did not set Sunday also
so Sunday =0
set Monday =Sunday (mean Monday is also 0)
set Wednesday = Sunday (mean Wednesday is also 0)

in this case .Net will pick the First one in which order they added to Enum

in your case


Sunday
Monday
Wednesday

any of above enum will print Sunday. Sunday is register with value 0 at first and you refer the same.

Sunday=0
Monday=0
Wednesday=0

Now if you just set value 0 to all of them (Sunday,Monday and Wednesday) it will show Wednesday because its set to value and all value are same to compiler pick last one.

If you refer or set from another enum it will pick the 1st reference value (if same dose not exists)

Another case:

I don't whether you test other enum's if you then you will find

Tuesday and Thursday it will always show Thursday because

see how compiler will define those values

Sunday=0,
Monday=0,
Tuesday=1,
Wednesday=0,
Thursday=1,
Friday=2,
Saturday=3

Ok now compile will show the last one in order which they added in enum.
So it will show Thursday

No to check the same just alternate the position of Tuesday and Thursday
then it will return Tuesday

Another Case :
C#
public enum Weekdays
    {
        Sunday,
        Monday = Sunday,
        Tuesday = Monday,
        Wednesday = Tuesday,
        Thursday = Wednesday,
        Friday = Thursday,
        Saturday = Friday
    }


Now it will print Wednesday

In this scenario .net will pick the middle one. as there were 7 item it will pick 4th if there 6 item it will pick 3 :)

Conclusion always set some value against enum.

Enjoy...
 
Share this answer
 
v2

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