C# | Cast an int to enum

using System;
					
public class Program
{
    public static void Main()
    {
        var e0 = (Food)Enum.ToObject(typeof(Food), 0);
        Console.WriteLine(e0);
        
        // or simply

        var e2 = (Food)2;
        Console.WriteLine(e2);        
    }

    public enum Food     
    {
        Pizza,
        Pasta,
        Tofu
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *