Numbered enum vs Unnumbered enum

Given these two listings:

public enum test
{
    one = 1,
    two,
    three
}

public enum test2
{
    zero = 0,
    one = 1,
    two = 2,
    three = 3
}

I prefer to use the first method. I see that many open source projects and code in my work use the second method in which each element matters. I rarely see the first example ever encoded. Is there a reason for this? Why is the second approach preferable to the first? In addition, you can change the title of this question to a more appropriate title, as I am not sure how to formulate this question

+3
source share
5 answers

Enumeration elements have names and values. It is almost all about value control. There are many ways to list, and in some of them you care about values.

Here are some examples:

  • XmlSerializer, () . .
  • , , . , . , ( ) .
  • enum (enum , , iTextSharp , , , - ), .

, . , , ,

public enum test
{
    one,
    two,
    three
}

, four oneandhalf , :

public enum test
{
    one,
    two,
    three,
    four,
    oneandhalf
}

public enum test
{
    one = 0,
    oneandhalf = 4,
    two = 1,
    three = 2,
    four = 3,
}

- ( ). , , - bad .

+2

. , , WCF. , WCF , .

+2

, , , , - , . , - two, three 2. ...

+2

public enum test2
{
    zero = 0,
    one = 5,
    two = 10,
    three = 20
}

, ...

0

Consider the following 2 listings, and you will see which one is more clear:

public enum numbers
{
    one = 5,
    hundred = 100,
    thousand = 1000
}

and

public enum numbers
{
    one,
    hundred,
    thousand
}

You can still use the second approach, but I think you will see what potential problems may arise.

0
source

All Articles