C# Enum Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To convert an int
to an enum in C#, you can use the Enum.ToObject()
method or a direct cast. Here's an example using both approaches:
using System; public enum Colors { Red, Green, Blue, Yellow, Orange } class Program { static void Main(string[] args) { int intValue = 2; // Using Enum.ToObject Colors color1 = (Colors)Enum.ToObject(typeof(Colors), intValue); Console.WriteLine($"Using Enum.ToObject: {color1}"); // Using direct cast Colors color2 = (Colors)intValue; Console.WriteLine($"Using direct cast: {color2}"); } }
In this example, the Colors
enum has five members. The intValue
variable is set to 2
. The Enum.ToObject()
method is called with the typeof(Colors)
argument and the intValue
to convert the int
value to a Colors
enum member. The direct cast approach simply casts the intValue
to the Colors
enum type.
Both approaches will output:
Using Enum.ToObject: Blue Using direct cast: Blue
Keep in mind that when converting an int
to an enum, the value should be valid for the enum type. If the value doesn't correspond to a valid enum member, it will still be converted, but the result might not make sense in the context of your application. To check if the integer value is a valid enum member, you can use the Enum.IsDefined()
method.
C# convert int to enum example: Converting an integer to an enum in C#.
int intValue = 2; DaysOfWeek day = (DaysOfWeek)intValue;
How to cast int to enum in C#: Casting an integer to an enum in C#.
int intValue = 2; DaysOfWeek day = (DaysOfWeek)intValue;
Convert numeric value to enum in C#: Converting a numeric value to an enum in C#.
int numericValue = 1; Status status = (Status)numericValue;
Parsing int to enum using Enum.Parse in C#:
Parsing an integer to an enum using Enum.Parse
in C#.
int intValue = 2; DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), intValue.ToString());
C# enum from int without casting: Creating an enum from an integer without explicit casting.
int intValue = 2; DaysOfWeek day = Enum.ToObject(typeof(DaysOfWeek), intValue) as DaysOfWeek;
Using Enum.IsDefined for int to enum conversion in C#:
Checking if an integer value is defined in the enum using Enum.IsDefined
before conversion.
int intValue = 2; if (Enum.IsDefined(typeof(DaysOfWeek), intValue)) { DaysOfWeek day = (DaysOfWeek)intValue; }
Convert int to enum with TryParse in C#:
Using Enum.TryParse
to convert an integer to an enum safely.
int intValue = 2; if (Enum.TryParse(typeof(DaysOfWeek), intValue.ToString(), out DaysOfWeek day)) { // Conversion successful }
C# enum type conversion from int: Converting an integer to an enum using explicit type conversion.
int intValue = 2; DaysOfWeek day = (DaysOfWeek)intValue;
Mapping int values to enum in C#: Creating a mapping function to convert int values to an enum.
int intValue = 2; DaysOfWeek day = MapIntToEnum(intValue); // ... private static DaysOfWeek MapIntToEnum(int value) { // Logic to map int to enum return (DaysOfWeek)value; }
C# explicit and implicit casting from int to enum: Demonstrating explicit and implicit casting from int to enum.
int intValue = 2; // Explicit casting DaysOfWeek explicitDay = (DaysOfWeek)intValue; // Implicit casting (with caution) DaysOfWeek implicitDay = intValue; // Use with caution, may result in unexpected behavior