Skip to main content

How to Give Enum Members Explicit Values in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Quarter = 25 gives the enum member Quarter the number 25 instead of the 0, 1, 2 that C# counts out on its own. Choose the numbers yourself when they mean something outside your code, such as cents, codes or a scale that starts at 1.

Microsoft's reference: You can also explicitly specify the associated constant values of an enum's members. The C# guide says when: Use explicit values when the numbers have external meaning, such as HTTP status codes or protocol identifiers.

Syntax​

Syntax
enum <Name>
{
<Member1> = <value1>,
<Member2> = <value2>,
<Member3>,
}
PartWhat it is
<Member1> = <value1>Gives the member the number <value1>, a whole number such as 25
<Member3> with no valueGets the number of the member before it, plus one

The C# specification on members without a value: If the enum member is the first enum member declared in the enum type, its associated value is zero. Every other one gets the number before it plus one.

The Example​

A console app called EnumValues gives four US coins their worth in cents as enum values, then adds up the coins in a purse.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n EnumValues
cd EnumValues
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/EnumValues/EnumValues.csproj:
Determining projects to restore...
Restored /Users/you/source/EnumValues/EnumValues.csproj (in 47 ms).
Restore succeeded.

dotnet new console -n EnumValues creates a folder named EnumValues with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or EnumValues.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the purse and the loop:

Program.cs (the purse)
Coin[] purse = [Coin.Quarter, Coin.Dime, Coin.Nickel, Coin.Penny];

int total = 0;

foreach (Coin coin in purse)
{
Console.WriteLine($"{coin} is worth {(int)coin}");
total += (int)coin;
}

Console.WriteLine($"The purse holds {total} cents");
  • [Coin.Quarter, Coin.Dime, Coin.Nickel, Coin.Penny] puts four coins in an array.
  • (int)coin converts a coin to its number, the value you gave it. The loop prints that number and adds it to total.

Then the enum, at the end of the file:

Program.cs (the enum)
// Each member's value is the coin's worth in cents.
enum Coin
{
Penny = 1,
Nickel = 5,
Dime = 10,
Quarter = 25,
}

Each member has = and its number after its name. The numbers don't have to follow each other: 1, 5, 10 and 25 are fine.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Quarter is worth 25
Dime is worth 10
Nickel is worth 5
Penny is worth 1
The purse holds 41 cents

macOS-style terminal window titled Terminal — EnumValues showing dotnet run printing Quarter is worth 25, Dime is worth 10, Nickel is worth 5, Penny is worth 1 and The purse holds 41 cents

Every coin printed the number written next to it in the enum, not its position: Quarter is the fourth member but printed 25. The four values add up to 41 cents. The reference on the conversion: If you cast an enum value to its underlying type, the result is the associated integral value of an enum member.

The Complete File​

macOS-style code window titled Program.cs — EnumValues showing an array of four coins, a loop that prints each coin with its value cast to int and adds it to a total, a line that prints the total in cents, and the enum Coin with Penny = 1, Nickel = 5, Dime = 10 and Quarter = 25

Program.cs
Coin[] purse = [Coin.Quarter, Coin.Dime, Coin.Nickel, Coin.Penny];

int total = 0;

foreach (Coin coin in purse)
{
Console.WriteLine($"{coin} is worth {(int)coin}");
total += (int)coin;
}

Console.WriteLine($"The purse holds {total} cents");

// Each member's value is the coin's worth in cents.
enum Coin
{
Penny = 1,
Nickel = 5,
Dime = 10,
Quarter = 25,
}

Common Mistakes​

MistakeWhat happensDo this instead
Giving a member a number that the member before it already got, as in Small = 1, Medium, Large = 2Medium counted on to 2, so Medium and Large were both 2: Size.Large printed Medium, and Size.Medium == Size.Large was TrueGive every member its own number. The ToString reference on shared numbers: your code should not make any assumptions about which name the method will return.
Leaving out a member for 0Coin nothing = default; printed 0, a value with no name, and so did both items of a new Coin[2] arrayAdd a member for 0, such as None = 0: then default printed None. The reference: Almost always define a member with value 0 in your enums.
Writing the value in quotes, as in Penny = "1"error CS0029: Cannot implicitly convert type 'string' to 'int'Write the number without quotes. An enum's members are numbers: the reference calls it a value type defined by a set of named constants of the underlying integral numeric type.

FAQ​

What does a member without a value get?​

One more than the member before it. In enum Level { Low = 1, Medium, High }, Low printed 1, Medium 2 and High 3. That's how to start a scale at 1 and let the rest count on. The C# specification: the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one.

How do I get the member back from a number?​

Cast the number to the enum. With stored holding 10, (Coin)stored printed Dime. The reference: For any enumeration type, explicit conversions exist between the enumeration type and its underlying integral type.

Conclusion​

Write Member = value to give an enum member the number you choose. A member without a value counts on from the one before it, so = 1 on the first member starts a scale at 1. Keep the numbers different from each other, and give 0 a member when the default value should have a name.

Additional Resources​