【转】深入浅出OOP(六): 理解C#的Enums
MSDN定義:枚舉類型(也稱為枚舉)為定義一組可以賦給變量的命名整數常量提供了一種有效的方法。? 例如,假設您必須定義一個變量,該變量的值表示一周中的一天。
該變量只能存儲七個有意義的值。 若要定義這些值,可以使用枚舉類型。枚舉類型是使用 enum 關鍵字聲明的。
?
從OOP上來說,枚舉的角色和和class一樣,它創建了一種新的數據類型。
1: namespace Enums 2: { 3: class Program 4: { 5: static void Main(string[] args) 6: { 7: } 8: } 9: 10: enum Color 11: { 12: Yellow, 13: Blue, 14: Brown, 15: Green 16: } 17: }上面的代碼,我們使用enum的關鍵字,創建了新的數據類型Color,并包含4個值:Yellow,?Blue,?Brown和Green。下面的例子我們給予Color枚舉。
?
直接輸出枚舉,則可得到枚舉的字符
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine(Color.Yellow); 9: Console.ReadLine(); 10: } 11: } 12: 13: enum Color 14: { 15: Yellow, 16: Blue, 17: Brown, 18: Green 19: } 20: }運行程序,輸出:
Yellow
強轉為int型,輸出試試看:
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((int)Color.Yellow); 9: Console.ReadLine(); 10: } 11: } 12: 13: enum Color 14: { 15: Yellow, 16: Blue, 17: Brown, 18: Green 19: } 20: }結果輸出:
0
?
從上面的例子中,我們可以看到枚舉的使用,如同static變量一樣,可被直接使用。如不用轉換則默認輸出枚舉定義的字符,強轉后
則輸出枚舉對應的數字值---故枚舉可表達恒量數值,或者命名的字符串標示。
?
基礎數據類型
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((byte)Color.Yellow); 9: Console.WriteLine((byte)Color.Blue); 10: Console.ReadLine(); 11: } 12: } 13: 14: enum Color:byte 15: { 16: Yellow, 17: Blue, 18: Brown, 19: Green 20: } 21: }?
結果輸出為:
0
1
這里唯一做的修改是枚舉Color繼承自byte ,而不是默認的int型。
枚舉可繼承自數值型類型,如long,?ulong,?short,?ushort,?int,?uint,?byte?何sbyte。但是無法繼承自char類型。
?
枚舉可被枚舉繼承嗎?
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((byte)Color.Yellow); 9: Console.WriteLine((byte)Color.Blue); 10: Console.ReadLine(); 11: } 12: } 13: 14: enum Color:byte 15: { 16: Yellow, 17: Blue, 18: Brown, 19: Green 20: 21: } 22: 23: enum Shades:Color 24: { 25: 26: } 27: }編譯,報錯:
Type?byte,?sbyte,?short,?ushort,?int,?uint,?long, or?ulong?expected.
枚舉可被class繼承嗎?
1: enum Color:byte 2: { 3: Yellow, 4: Blue, 5: Brown, 6: Green 7: } 8: 9: class Derived:Color 10: { 11: 12: }?
編譯報錯:
'Enums.Derived': cannot derive from sealed type 'Enums.Color'
?
接下來,我們看看枚舉和這3個接口的關系:IComparable,?IFormattable 和IConvertible。
A. IComparable
1: using System; 2: 3: namespace Enums 4: { 5: internal enum Color 6: { 7: Yellow, 8: Blue, 9: Green 10: } 11: 12: internal class Program 13: { 14: private static void Main(string[] args) 15: { 16: Console.WriteLine(Color.Yellow.CompareTo(Color.Blue)); 17: Console.WriteLine(Color.Blue.CompareTo(Color.Green)); 18: Console.WriteLine(Color.Blue.CompareTo(Color.Yellow)); 19: Console.WriteLine(Color.Green.CompareTo(Color.Green)); 20: Console.ReadLine(); 21: } 22: } 23: }結果輸出:
-1
-1
1
0
-1表示小于關系,0表示等于關系,1表示大于關系。這里標明了enum默認繼承了IComparable接口,故有CompareTo()函數。
B. IFormattable
1: using System; 2: 3: namespace Enums 4: { 5: internal enum Color 6: { 7: Yellow, 8: Blue, 9: Green 10: } 11: 12: internal class Program 13: { 14: private static void Main(string[] args) 15: { 16: System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "X")); 17: System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "d")); 18: Console.ReadLine(); 19: } 20: } 21: }結果輸出:
00000002
2
Format方法繼承自IFormatter 接口,它是一個static函數,因此可以被枚舉Color直接使用。format需要傳入3個參數,第一個是枚舉的類型,
第二個參數是枚舉值,第三個是格式化標示---二進制、十進制等。
C. IConvertible
1: Hide Copy Code 2: using System; 3: 4: namespace Enums 5: { 6: enum Color 7: { 8: Yellow, 9: Blue, 10: Green 11: } 12: 13: internal class Program 14: { 15: private static void Main(string[] args) 16: { 17: string[] names; 18: names = Color.GetNames(typeof (Color)); 19: foreach (var name in names) 20: { 21: Console.WriteLine(name); 22: } 23: Console.ReadLine(); 24: } 25: } 26: } 27:結果輸出:
Yellow
Blue
Green
GetNames函數是枚舉Color的靜態方法,用于獲得枚舉所有的字符標示名稱集合。
同理也可使用ToString輸出枚舉的字符標示:
1: using System; 2: 3: namespace Enums 4: { 5: enum Color 6: { 7: Yellow, 8: Blue, 9: Green 10: } 11: 12: internal class Program 13: { 14: private static void Main(string[] args) 15: { 16: Console.WriteLine(Color.Blue.ToString()); 17: Console.WriteLine(Color.Green.ToString()); 18: Console.ReadLine(); 19: } 20: } 21: }?
顯示輸出:
Blue
Green
上面的例子顯示,枚舉可在int和string直接轉換,這個特性是枚舉使用中非常重要的一個功能。
?
試試看,枚舉的字符標示是否可以重復定義:
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((byte)Color.Yellow); 9: Console.WriteLine((byte)Color.Blue); 10: Console.ReadLine(); 11: } 12: } 13: 14: enum Color 15: { 16: Yellow, 17: Blue, 18: Brown, 19: Green, 20: Blue 21: } 22: }編譯報錯,結果:
Compile time error: The type 'Enums.Color' already contains a definition for 'Blue'
可見枚舉中不能定義重復的字符標示。
?
再看另外一個例子:
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((int)Color.Yellow); 9: Console.WriteLine((int)Color.Blue); 10: Console.WriteLine((int)Color.Brown); 11: Console.WriteLine((int)Color.Green); 12: 13: Console.ReadLine(); 14: } 15: } 16: 17: enum Color 18: { 19: Yellow =2, 20: Blue, 21: Brown=9, 22: Green, 23: 24: } 25: }結果:
2
3
9
10
?
?
從結果看,我們可以在枚舉定義的時候重新指定數值,如我們指定了yellow為2,則Blue默認為Yellow+1,為3. 下來,我們指定了Brown為9,則
其下的Green為Brown + 1,為10。 這是一個有趣的enum特性。
?
如指定的數據類型超過枚舉的定義類型,如何?
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: 9: } 10: } 11: 12: enum Color:byte 13: { 14: Yellow =300 , 15: Blue, 16: Brown=9, 17: Green, 18: } 19: }編譯報錯:
Compile time error: Constant value '300' cannot be converted to a 'byte'
300超出了byte數據類型的范圍,故報錯。 枚舉的類型檢測非常好,在項目使用中很實用的功能。
?
枚舉引用代碼
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Console.WriteLine((int)Color.Yellow); 9: Console.WriteLine((int)Color.Blue); 10: Console.WriteLine((int)Color.Brown); 11: Console.WriteLine((int)Color.Green); 12: 13: Console.ReadLine(); 14: } 15: } 16: 17: enum Color 18: { 19: Yellow = 2, 20: Blue, 21: Brown = 9, 22: Green = Yellow 23: } 24: }結果輸出:
2
3
9
2
這里,我們定義Green的值,引用了Color的Yellow枚舉值。
枚舉,是否可以在外面修改枚舉值:
1: using System; 2: namespace Enums 3: { 4: class Program 5: { 6: static void Main(string[] args) 7: { 8: Color.Yellow = 3; 9: } 10: } 11: 12: enum Color 13: { 14: Yellow = 2, 15: Blue, 16: Brown = 9, 17: Green = Yellow 18: } 19: }運行結果:
Compile time error: The left-hand side of an assignment must be a variable, property or indexer
編譯報錯了。可見枚舉數值是常量,僅在初始化的時候確定,外部無法動態修改。
?
那么,枚舉是否可以循環依賴?
1: using System; 2: 3: namespace Enums 4: { 5: internal enum Color 6: { 7: Yellow=Blue, 8: Blue 9: } 10: 11: internal class Program 12: { 13: private static void Main(string[] args) 14: { 15: } 16: } 17: }編譯結果:
Compile time error: The evaluation of the constant value for 'Enums.Color.Yellow' involves a circular definition
保留關鍵字
1: using System; 2: 3: namespace Enums 4: { 5: enum Color 6: { 7: value__ 8: } 9: 10: internal class Program 11: { 12: private static void Main(string[] args) 13: { 14: 15: } 16: } 17: }編譯報錯:
Compile time error: The enumerator name 'value__' is reserved and cannot be used
原因很簡單,這里的value__是保留關鍵字。
枚舉小結:
?
總結
以上是生活随笔為你收集整理的【转】深入浅出OOP(六): 理解C#的Enums的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】2.1【MySQL】运行原理(一)
- 下一篇: 海王娘娘否认剧情杀