Converting an Enum to Integer in C#


This content was proof-read by Grammarly. You can Try Grammary for FREE

If you need to convert an enum to its numeric equivalent , you can simply cast the values to integer .

Below is a sample code snippet that demonstrates how to do it.


Are you a .NET Developer planning to integrate GraphQL for .NET in your Application? Check out this book BUY & DOWNLOAD GRAPHQL.NET COOKBOOK

Converting an Enum to Integer in C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AbundantcodeConsoleApp
{
    enum EmployeeDesignation
    {
        Trainee=1,
        SoftwareEngineer = 2,
        SeniorSoftwareEngineer=5
    };
    internal class Program
    {
        private static void Main(string[] args)
        {
            int design = (int)EmployeeDesignation.SoftwareEngineer;
            Console.WriteLine(design);
            Console.ReadLine();
        }     
    }
    
}
1
%d bloggers like this: