---
title: "How to cast int to enum instead of using switch?"  
description: "How to cast int to enum instead of using switch?"  
author: "Utpal Vishwas"  
published: 2023-08-21  
updated: 2023-08-23  
canonical: https://www.mindstick.com/forum/159629/how-to-cast-int-to-enum-instead-of-using-switch  
category: "c#"  
tags: ["c#", "java", "enum"]  
reading_time: 2 minutes  

---

# How to cast int to enum instead of using switch?

How to [cast int to enum](https://www.mindstick.com/forum/159628/how-can-i-cast-int-to-enum-in-java) [instead of](https://www.mindstick.com/articles/330/triggers-in-sql-server) using [switch](https://www.mindstick.com/blog/104495/switch-bringing-new-advancements-in-the-education-management-system)?

## Replies

### Reply by Aryan Kumar

Yes, you can [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](https://www.mindstick.com/forum/159626/how-can-i-cast-a-string-to-an-enum) instead of using a switch statement. Here's how you can do it in C#:

C#

```plaintext
enum MyEnum {
  Value1,
  Value2,
  Value3
}

int myInt = 1;

// Cast the int to an enum.
MyEnum myEnum = (MyEnum)myInt;
```

In this example, the `myInt` variable is an int that has the value 1. The `myEnum` variable is an enum that is initialized to the value of `Value1`. The `(MyEnum)myInt` cast operator casts the `myInt` variable to an enum.

You can also use the `switch` statement to cast an int to an enum. Here's how you can do it:

C#

```plaintext
enum MyEnum {
  Value1,
  Value2,
  Value3
}

int myInt = 1;

switch (myInt) {
  case 1:
    myEnum = MyEnum.Value1;
    break;
  case 2:
    myEnum = MyEnum.Value2;
    break;
  case 3:
    myEnum = MyEnum.Value3;
    break;
  default:
    myEnum = MyEnum.Value1;
    break;
}
```

In this example, the `myInt` variable is an int that has the value 1. The `switch` statement checks the value of the `myInt` variable and assigns the corresponding enum value to the `myEnum` variable.

Which method you use depends on your specific needs. If you need to cast an int to an enum and you don't need to do any further processing on the enum value, then the cast operator is a good option. If you need to do further processing on the enum value, then the `switch` statement is a good option.


---

Original Source: https://www.mindstick.com/forum/159629/how-to-cast-int-to-enum-instead-of-using-switch

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
