---
title: "How do I cast int to enum in C#?"  
description: "How do I cast int to enum in C#?"  
author: "Sandra Emily"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159564/how-do-i-cast-int-to-enum-in-c-sharp  
category: "c#"  
tags: ["c#", "datatype"]  
reading_time: 2 minutes  

---

# How do I cast int to enum in C#?

How do I [cast int to enum](https://www.mindstick.com/forum/159628/how-can-i-cast-int-to-enum-in-java) in C#?

## Replies

### Reply by Aryan Kumar

To [cast](https://answers.mindstick.com/qa/97273/how-to-cast-a-baitcaster) an [int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) to an [enum in C#](https://www.mindstick.com/forum/33625/how-to-use-enum-in-c-sharp-dot-net), you can use the `cast` operator. The cast operator is a pair of round brackets with the type that you want to cast to. For example, the following code casts the integer 5 to the enum `Color.Red`:

C#

```plaintext
enum Color { Red, Green, Blue }

Color red = (Color)5;
```

In this code, the `cast` operator is used to cast the integer 5 to the enum `Color`. The result is the `Color.Red` enum.

It is important to note that the enum must have an underlying type of int for this to work. If the enum has a different underlying type, such as a `string` or a `DateTime`, you will get an error.

You can also use the `Enum.Parse()` method to cast an int to an enum. The `Enum.Parse()` method takes the enum type and the integer value as its parameters and returns the corresponding enum value. The following code is equivalent to the previous code:

C#

```plaintext
enum Color { Red, Green, Blue }

Color red = Enum.Parse(typeof(Color), 5);
```

In this code, the `Enum.Parse()` method is used to cast the integer 5 to the enum `Color`. The first parameter is the type of the enum, and the second parameter is the integer value to be converted.


---

Original Source: https://www.mindstick.com/forum/159564/how-do-i-cast-int-to-enum-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
