---
title: "How can I cast int to enum in C#?"  
description: "How can I cast int to enum in C#?"  
author: "Steilla Mitchel"  
published: 2023-07-12  
updated: 2023-07-13  
canonical: https://www.mindstick.com/forum/159044/how-can-i-cast-int-to-enum-in-c-sharp  
category: "c#"  
tags: ["c#", "class"]  
reading_time: 2 minutes  

---

# How can I cast int to enum in C#?

How can 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

Sure, here is how you can [cast](https://answers.mindstick.com/qa/97273/how-to-cast-a-baitcaster) [int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) to [enum in C#](https://www.mindstick.com/forum/33625/how-to-use-enum-in-c-sharp-dot-net):

C#

```plaintext
using System;

public enum Colors
{
    Red,
    Green,
    Blue
}

public class CastIntToEnum
{
    public static void Main(string[] args)
    {
        // Create an int.
        int myInt = 1;

        // Cast the int to an enum.
        Colors myColor = (Colors)myInt;

        // Print the enum.
        Console.WriteLine("The enum is {0}.", myColor);
    }
}
```

This code first creates an int. The int is assigned the value 1.

The code then casts the int to an enum. The `(Colors)myInt` cast operator is used to cast the int to an enum of type `Colors`.

Finally, the code prints the enum to the console.

Here is an explanation of the code:

- The `using` statement is used to import the `System` namespace. The `System` namespace contains the `Enum` class.
- The `Enum` class is used to represent enumerations.
- The `Colors` enum is an enumeration that represents the colors red, green, and blue.
- The `(Colors)myInt` cast operator is used to cast the int to an enum of type `Colors`.
- The `Console.WriteLine()` method is used to print a string to the console.


---

Original Source: https://www.mindstick.com/forum/159044/how-can-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.
