enum

 

enum 特性詳列如下

一、

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Day day = Day.Friday;

            switch (day)
            {
                case Day.Monday:
                    Console.WriteLine("猴子穿新衣");
                    break;
                case Day.Tuesday:
                    Console.WriteLine("猴子肚子餓");
                    break;
                case Day.Wednesday:
                    Console.WriteLine("猴子去爬山");
                    break;
                case Day.Thursday:
                    Console.WriteLine("猴子看電視");
                    break;
                case Day.Friday:
                    Console.WriteLine("猴子去跳舞");
                    break;
                case Day.Saturday:
                    Console.WriteLine("猴子去斗六");
                    break;
                case Day.Sunday:
                    Console.WriteLine("猴子過生日");
                    break;
            }
        }
    }

    public enum Day
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
}

使用 enum 有兩個好處

a、你可以利用現有的 enum 元素來指定合法的值給變數而不會給錯,如:Day day = Day.Friday;

b、在 Visual Studio,IntelliSense 可列出有哪些元素可以選

 

二、enum 可以存在於 class 或 struct 裡

using System;

namespace ConsoleApp1
{
    class Program
    {
        public enum Day
        {
            Sunday,
            Monday,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday
        }

        static void Main(string[] args)
        {
            if (Day.Sunday == 0)
            {
                Console.WriteLine(Day.Sunday);
            }
        }
    }
}

 

三、每個 enum 型別都有一個基礎型別 (underlying type),可以是任何整數型別(integral numeric type),

如:sbyte、byte、short、ushort、int、uint、long、ulong。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Day.Sat == 0)
            {
                Console.WriteLine(Day.Sat);
            }
        }
    }

    enum Day : byte
    {
        Sat,
        Sun,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri
    };
}

 

四、char 型別不能作為 enum 的基礎型別。

 

五、enum 預設的基礎型別為 int。

 

六、enum 可以指定初始值,之後會 +1 遞增。預設值為 0。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((byte)Day.Sun);
        }
    }

    enum Day : byte
    {
        Sat = 10,
        Sun,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri
    };
}

 

七、若想取得 enum 值,可以強制轉型到該基礎型別

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((byte)Day.Sun);
        }
    }

    enum Day : byte
    {
        Sat = 10,
        Sun,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri
    };
}

 

八、對於每一個 enum 元素,可以特別指定值給他;一個 enum 元素可以成為另一個 enum 元素值的相加。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((int)MachineState.Hibernating);
        }
    }

    enum MachineState
    {
        PowerOff = 0,
        Running = 5,
        Sleeping = 10,
        Hibernating = Sleeping + 5
    }
}

 

九、enum 值可以用 n 進制表示

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((char)Punctuation.Semicolon);
        }
    }

    public enum Punctuation
    {
        Comma = 0x2c,
        Period = 0x2e,
        Semicolon = 0x3b,
        Colon = 0x3a
    }
}

 

十、Enumeration types as bit flags

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // The bitwise OR of 0001 and 0100 is 0101.
            CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;

            // Because the Flags attribute is specified, Console.WriteLine displays
            // the name of each enum element that corresponds to a flag that has
            // the value 1 in variable options.
            Console.WriteLine(options);
            // The integer value of 0101 is 5.
            Console.WriteLine((int)options);
        }
    }

    // Add the attribute Flags or FlagsAttribute.
    [Flags]
    public enum CarOptions
    {
        // The flag for SunRoof is 0001.
        SunRoof = 0x01,
        // The flag for Spoiler is 0010.
        Spoiler = 0x02,
        // The flag for FogLights is 0100.
        FogLights = 0x04,
        // The flag for TintedWindows is 1000.
        TintedWindows = 0x08,
    }
}

 

參考資料:

enum (C# Reference)

Enumeration types (C# Programming Guide)

Enum Class

FlagsAttribute Class