Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

How to Convert Int to Enum in C#

Here you will learn how to convert Int to Enum in C#.

Convert int to Enum by Type Casting

You can explicitly type cast an int to a particular enum type, as shown below.

Example: Type Cast int to Enum
public enum Week
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

class Program
{
    static void Main(string[] args)
    {
        int i = 2, j = 6, k = 10;
		Week day1, day2, day3;
		
		day1 = (Week)i; //Wednesday
		day2 = (Week)j; //Sunday
		day3 = (Week)k; //10
    }
}
Try it

Convert int to Enum using Enum.ToObject() Method

Use the Enum.ToObject() method to convert integers to enum members, as shown below.

Example: Convert int to Enum using Enum.ToObject()
int i = 2, j = 6, k = 10;
Week day1, day2, day3;
		
day1 = (Week)Enum.ToObject(typeof(Week), i); //Wednesday
day2 = (Week)Enum.ToObject(typeof(Week), j); //Sunday
day3 = (Week)Enum.ToObject(typeof(Week), k); //10
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.