Question
I have a class called Questions. Inside this class, there is an enum named Question defined like this:
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
In the Questions class, I also have a method like Get(int foo) that returns a Questions object for the given integer value.
Is there a simple way to get the integer value from the enum so I can call something like this?
Questions.Get(Question.Role)
If needed, how should I convert the enum value so it works with a method that expects an int?
Short Answer
By the end of this page, you will understand how enums work in C#, how to convert an enum value to its underlying integer value, and when it is better to accept the enum directly instead of using int parameters.
Concept
In C#, an enum is a named set of constant values. It lets you replace raw numbers like 2 or 5 with meaningful names like Role or NumberOfServers.
That makes code easier to read, write, and maintain.
For example, this is harder to understand:
Questions.Get(2);
This is much clearer:
Questions.Get((int)Question.Role);
An enum value has an underlying numeric type. In C#, the default underlying type is int unless you specify something else.
So in your example:
Question.Role
has the numeric value 2.
To get that integer value, you usually cast the enum to int:
(int)Question.Role
This matters in real programming because enums help avoid "magic numbers". Instead of passing unexplained integers around your codebase, you use named values that document intent.
Mental Model
Think of an enum like a labeled list of IDs.
Roleis a label2is the stored ID behind that label
The label makes the code human-friendly. The number makes it machine-friendly.
Casting the enum to int is like saying: "Give me the numeric ID behind this label."
So:
Question.Role= the label(int)Question.Role= the numeric ID
Syntax and Examples
Basic enum syntax
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
Convert enum to int
int id = (int)Question.Role;
Console.WriteLine(id); // 2
Use it with a method that expects int
public class Questions
{
public static Questions Get(int foo)
{
Console.WriteLine($"Looking up question {foo}");
return new Questions();
}
}
Questions result = Questions.Get((int)Question.Role);
Better approach: accept the enum directly
public class Questions
{
public Questions ()
{
Console.WriteLine();
Questions();
}
}
Questions result = Questions.Get(Question.Role);
Step by Step Execution
Consider this example:
public enum Question
{
Role = 2,
ProjectFunding = 3
}
public class Questions
{
public static void Get(int foo)
{
Console.WriteLine($"Question id: {foo}");
}
}
Questions.Get((int)Question.Role);
Step by step
- The enum
Questionis defined. Question.Rolerepresents the named enum memberRole.- Its underlying integer value is
2. - The cast
(int)Question.Roleconverts the enum value into the integer2. - The method call becomes effectively:
Questions.Get(2);
- Inside
Get,fooreceives the value .
Real World Use Cases
Enums are commonly used when your program works with a fixed set of known values.
Common examples
- Database IDs with meaning
Question.Role = 2Question.ProjectFunding = 3
- Application status values
Pending,Approved,Rejected
- User roles
Admin,Editor,Viewer
- API request types
Create,Update,Delete
- Configuration options
Development,Testing,Production
In your case
Your enum likely maps named questions to stored numeric IDs. That is useful when:
Real Codebase Usage
In real projects, developers often use enums in a few common patterns.
1. Method parameters use enums instead of ints
public Questions Get(Question question)
{
return LoadById((int)question);
}
This keeps the public API clean while still allowing internal numeric lookup.
2. Validation when converting from int
If you receive a raw integer from a database, API, or form input, validate it before treating it as an enum value.
int input = 3;
if (Enum.IsDefined(typeof(Question), input))
{
Question q = (Question)input;
Console.WriteLine(q); // ProjectFunding
}
3. Guard clauses
public Questions Get(Question question)
{
if (!Enum.IsDefined(typeof(Question), question))
throw new ArgumentOutOfRangeException(nameof(question));
return LoadById((int)question);
}
4. Internal mapping to database values
Common Mistakes
1. Forgetting to cast when the method expects int
Broken code:
Questions.Get(Question.Role);
If there is only a Get(int foo) method, this will not match automatically.
Fix:
Questions.Get((int)Question.Role);
2. Using raw numbers instead of enum names
Less clear:
Questions.Get(2);
Better:
Questions.Get((int)Question.Role);
Best, if possible:
Questions.Get(Question.Role);
3. Assuming every integer is a valid enum value
Broken assumption:
Question q = (Question)999;
This compiles, but 999 may not be a defined enum member.
Safer approach:
Comparisons
| Approach | Example | Pros | Cons |
|---|---|---|---|
| Pass raw int | Questions.Get(2) | Simple | Harder to read, easy to misuse |
| Cast enum to int | Questions.Get((int)Question.Role) | Works with existing int method | Still exposes numeric API |
| Accept enum directly | Questions.Get(Question.Role) | Clear, type-safe, readable | May require changing method signature |
Enum vs const integers
| Option | Example | Best for |
|---|
Cheat Sheet
Quick reference
Define an enum
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4
}
Get the int value from an enum
int id = (int)Question.Role;
Pass enum to a method that expects int
Questions.Get((int)Question.Role);
Better method signature
public Questions Get(Question question)
{
return LoadById((int)question);
}
Convert int back to enum
Question q = (Question)2;
Validate before converting from int
bool valid = Enum.IsDefined(typeof(Question), 2);
Key rules
FAQ
Can I pass an enum directly to a method that takes int in C#?
Usually, you should cast it explicitly:
Questions.Get((int)Question.Role);
How do I get the numeric value of an enum in C#?
Cast it to int:
int value = (int)Question.Role;
Is it better to use int or enum as a method parameter?
If the method expects a fixed set of named values, using the enum is usually better because it is more readable and type-safe.
Can I convert an int back to an enum?
Yes:
Question q = (Question)2;
But validate first if the integer may be invalid.
What is the default underlying type of an enum in C#?
The default underlying type is int.
Why use enums instead of magic numbers?
Enums make code easier to understand. Question.Role is clearer than just 2.
Mini Project
Description
Build a small question lookup utility that uses an enum to represent question IDs and retrieves a question label based on the selected enum value. This demonstrates how enums make code clearer than passing raw integers around.
Goal
Create a C# program that accepts a Question enum value, converts it to its numeric ID internally, and returns a readable result.
Requirements
- Define a
Questionenum with explicit integer values. - Create a method that accepts a
Questionparameter. - Convert the enum to its integer value inside the method.
- Return a readable message showing both the enum name and numeric ID.
- Call the method with at least two different enum values.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
C# Type Checking Explained: typeof vs GetType() vs is
Learn when to use typeof, GetType(), and is in C#. Understand exact type checks, inheritance, and safe type testing clearly.
C# Version Numbers Explained: C# vs .NET Framework and Why “C# 3.5” Is Incorrect
Learn the correct C# version numbers, how they map to .NET releases, and why terms like C# 3.5 are inaccurate and confusing.