# 列舉類型 (c # 參考)

enum Season
{
Spring,
Summer,
Autumn,
Winter
}


enum ErrorCode : ushort
{
None = 0,
Unknown = 1,
ConnectionLost = 100,
}


## 作為位元旗標的列舉類型

[Flags]
public enum Days
{
None      = 0b_0000_0000,  // 0
Monday    = 0b_0000_0001,  // 1
Tuesday   = 0b_0000_0010,  // 2
Wednesday = 0b_0000_0100,  // 4
Thursday  = 0b_0000_1000,  // 8
Friday    = 0b_0001_0000,  // 16
Saturday  = 0b_0010_0000,  // 32
Sunday    = 0b_0100_0000,  // 64
Weekend   = Saturday | Sunday
}

public class FlagsEnumExample
{
public static void Main()
{
Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;
Console.WriteLine(meetingDays);
// Output:
// Monday, Wednesday, Friday

Days workingFromHomeDays = Days.Thursday | Days.Friday;
Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}"); // Output: // Join a meeting by phone on Friday bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday; Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}");
// Output:
// Is there a meeting on Tuesday: False

var a = (Days)37;
Console.WriteLine(a);
// Output:
// Monday, Wednesday, Saturday
}
}


## System.string 型別和列舉條件約束

System.Enum 類型是所有列舉類型的抽象基類。 它會提供數種方法來取得列舉型別和其值的相關資訊。 如需詳細資訊和範例，請參閱 System.Enum API 參考頁面。

## 轉換

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
}
}


Enum.IsDefined您可以使用方法來判斷列舉型別是否包含具有特定關聯值的列舉成員。