gotnull
gotnull

Reputation: 27214

Iterating through enum with EnumMember attribute

Given the following enum:

public enum JobTypes
{
    [EnumMember (Value = "IN PROGRESS")]
    IN_PROGRESS,
    SUBMITTED,
    [EnumMember (Value = "IN REVIEW")]
    IN_REVIEW
}

I am iterating through these as follows:

foreach (var jobType in Enum.GetValues (typeof(JobTypes))) {
    Console.WriteLine("{0,3:D} 0x{0:X} {1}", Enum.Parse(typeof(JobTypes), jobType.ToString()), jobType);
}

Output:

// The example displays the following output: 
// 0     0x00000000     IN_PROGRESS 
// 1     0x00000001     SUBMITTED
// 2     0x00000002     IN_REVIEW

Expected:

// The example displays the following output: 
// 0     0x00000000     IN PROGRESS // no _ character 
// 1     0x00000001     SUBMITTED
// 2     0x00000002     IN REVIEW // no _ character

Upvotes: 0

Views: 458

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500535

EnumMemberNameAttribute only affects serialization:

The EnumMemberAttribute enables fine control of the names of the enumerations as they are serialized.

It doesn't have any effect on the result of calling ToString() on the value, which is effectively what you're doing here.

Upvotes: 2

Related Questions