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.
In many cases, the best design is not to convert the enum to int at all. Instead, make your method accept the enum type directly:
Questions Get(Question question)
That gives you stronger type safety and cleaner code.
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 static Questions Get(Question question)
{
Console.WriteLine($"Looking up question {(int)question}");
return new Questions();
}
}
Questions result = Questions.Get(Question.Role);
This version is usually better because:
- the method clearly communicates what values it expects
- invalid random integers are less likely to be passed
- the call site is easier to read
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 value2. - The program prints:
Question id: 2
Trace example with a variable
Question selected = Question.ProjectFunding;
int id = (int)selected;
Questions.Get(id);
Execution flow:
selectedstoresQuestion.ProjectFundingidbecomes3Questions.Get(id)becomesQuestions.Get(3)
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:
- reading records from a database
- routing logic based on question type
- avoiding hardcoded numbers in business logic
- making code more self-documenting
Instead of writing:
Questions.Get(6);
you can write:
Questions.Get((int)Question.TopBusinessConcern);
or better:
Questions.Get(Question.TopBusinessConcern);
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
public Questions LoadById(int id)
{
// query database using id
return new Questions();
}
public Questions Get(Question question)
{
return LoadById((int)question);
}
This pattern gives you both:
- readable public code
- practical numeric storage underneath
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:
if (Enum.IsDefined(typeof(Question), 999))
{
Question q = (Question)999;
}
4. Designing APIs around int when an enum is clearer
Less expressive:
public Questions Get(int foo)
Better:
public Questions Get(Question question)
5. Forgetting that enums can have non-default values
In your enum, values start at 2, not 0.
public enum Question
{
Role = 2,
ProjectFunding = 3
}
Do not assume the first member is always 0.
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 |
|---|---|---|
enum | Question.Role | Fixed related named values |
const int | const int Role = 2; | Single standalone constants |
Enums are better when values belong to the same logical group.
Casting vs Convert.ToInt32
| Option | Example | Notes |
|---|---|---|
| Cast | (int)Question.Role | Most common and simplest |
| Convert | Convert.ToInt32(Question.Role) | Works, but usually unnecessary |
For normal enum-to-int conversion, casting is the standard approach.
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
- Default enum underlying type is
int - Use
(int)myEnumValueto get the numeric value - Prefer enum parameters over raw
intparameters when possible - Validate unknown integers before treating them as enum values
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.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.