返回
Featured image of post C♯ - 自訂類型(Custom types) - 列舉(Enum) 筆記

C♯ - 自訂類型(Custom types) - 列舉(Enum) 筆記

列舉(Enum) 筆記

列舉 Default 常數值為類型 int

官方基本範例

public enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

public class EnumConversionExample
{
    public static void Main()
    {
        Season a = Season.Autumn;
        Console.WriteLine($"Integral value of {a} is {(int)a}");
        // output: Integral value of Autumn is 2

        var b = (Season)1;
        Console.WriteLine(b);  // output: Summer

        var c = (Season)4;
        Console.WriteLine(c);  // output: 4
    }
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus