5

I read this statement in a C# book.

Enumerations do not necessarily need to follow a sequential ordering, and need not have unique values.

If I understand that statement, it means one of this is acceptable (I don't know which):

1.

enum EmpType
{
    Manager = 1,
    Grunt = 1,
    Contractor = 100,
    VicePresident = 9
}

2.

enum EmpType
{
    Manager = 10,
    Manager = 1,
    Contractor = 100,
    VicePresident = 9
}

Can someone please explain to me? I thought C# was supposed to be a subset of C/C++.

5
  • 1
    It cannot contain duplicate field names, like in snippet #2. Commented Jun 8, 2011 at 10:41
  • 5
    " I thought C# was supposed to be a subset of C/C++." who on earth said that? Commented Jun 8, 2011 at 10:43
  • C# has nothing common with C++ (except they are both OO languages) or C. C# is closer to Java or Object Pascal (Delphi). Commented Jun 8, 2011 at 10:48
  • @Petr And Java came from C, right? That's what the same book said. The book was published by Apress, so they should be right (or so I think). Commented Jun 8, 2011 at 11:01
  • Java have syntax similar to C (so does C# and C++ too) but I wouldn't say it came from C. Commented Jun 8, 2011 at 11:48

5 Answers 5

4

The first one would be valid, you may have duplicate Values not duplicate Names

Sign up to request clarification or add additional context in comments.

Comments

3

1 is correct, 2 is not.

As the book says, enums need not have unique values (example 2 shows enums with non-unique names). Names must be unique, as it is how the compiler matches it up to a value.

Comments

3

Actually - why not to check :) Each enum is subclass of System.Enum class (specially handled), each enum element is a static field initialized with a literal value - you cannot have two fields with the same names, but can have fields with the same values, so #1 will work, #2 won't.

Comments

1

Number 1 is acceptable. Number 2 throws a compile time exception. You can have multiple Equivalent values. But not equivalent duplicate names.

For example, suppose you want to define an enum for a companie's personnel job levels. You have staff, management. staff include sales department and IT department and it doesn't make any difference for you if a person is in sales or IT, He/she is considered staff anyway. You can define the following enum:

public enum PersonnelLevels
{   
    Management=0,
    Sales=1,
    IT=1
}  

Comments

0

Refering to enum (C# Reference):

The default underlying type of enumeration elements is int.

You can assign any integer value to any enumuration element. You can assign duplicated values to different elements. However, elements names must be unique.

That means, block one is correct. But, block two is not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.