枚舉 enum 類型轉換


枚舉(Enum)定義

       enum 關鍵字用於聲明枚舉,即一種由一組稱為枚舉數列表的命名常量組成的獨特類型。通常情況下,最好是在命名空間內直接定義枚舉,以便該命名空間中的所有類都能夠同樣方便地訪問它。 但是,還可以將枚舉嵌套在類或結構中。現在的有些電商網站根據購物的積分用到的,金牌會員,銀牌會員,銅牌會員。枚舉值獲值一般獲取的時候包括獲取變量和變量值,默認情況下,第一個枚舉數的值為 0,后面每個枚舉數的值依次遞增 1。直接使用Enum中的靜態方法即可操作.GetValues中獲取的是枚舉變量的值,類型是枚舉名,之后自動輸出的是枚舉名.每種枚舉類型都有基礎類型,該類型可以是除 char以外的任何整型(重點)。 枚舉元素的默認基礎類型為 int.准許使用的枚舉類型有 byte、sbyte、short、ushort、int、uint、long 或 ulong。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6     enum Color
 7     {
 8         Red,
 9         Green = 10,
10         Blue
11     }
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             Console.WriteLine(StringFromColor(Color.Red));
17             Console.WriteLine(StringFromColor(Color.Green));
18             Console.WriteLine(StringFromColor(Color.Blue));
19             Console.Read();
20         }
21         static string StringFromColor(Color c)
22         {
23             switch (c)
24             {
25                 case Color .Red :
26                     return String.Format("Red = {0}", (int)c);
27                 case Color.Green:
28                     return String.Format("Green = {0}", (int)c);
29                 case Color.Blue:
30                     return String.Format("Blue = {0}", (int)c);
31                 default 
32                     return "Invalid color";
33 
34             }
35         }
36     }
View Code


 接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace @interface
 7 {
 8     public interface IWindow
 9     {
10         Object GetMenu();
11     }
12     public interface IRestaurant
13     {
14         Object GetMenu();
15     }
16 
17     // 該類型繼承自system.Object,並實現了IWindow和IRestaurant接口
18     public class GiuseppePizzaria : IWindow, IRestaurant
19     {
20         // 該方法包括了IWindow接口的GetMenu方法實現
21         Object IWindow.GetMenu()
22         {
23             return "IWindow.GetMenu";
24         }
25 
26         // 該方法包括了IRestaurant接口的GetMenu方法實現
27         Object IRestaurant.GetMenu()
28         {
29             return "IRestaurant.GetMenu";
30         }
31 
32         //這個GetMenu方法與接口沒有任何關系
33         public Object GetMenu()
34         {
35             return "GiuseppePizzaria.GetMenu";
36         }
37    
38         static void Main(string[] args)
39         {
40             // 構造一個實例類
41             GiuseppePizzaria gp = new GiuseppePizzaria();
42             Object menu;
43 
44             //調用公有的GetMenu方法。使用GiuseppePizzaria引用,
45             // 顯示接口成員將為私有方法,因此不可能被調用
46             menu = gp.GetMenu();
47             Console.WriteLine(menu);
48 
49             //調用IWindow的GetMenu方法。使用IWindow引用,
50             // 因此只有IWindow.GetMenum方法被調用
51             menu = ((IWindow)gp).GetMenu();
52             Console.WriteLine(menu);
53 
54             //調用IRestaurant的GetMenu方法。使用IRestaurant引用,
55             // 因此只有IRestaurant.GetMenum方法被調用
56             menu = ((IRestaurant)gp).GetMenu();
57             Console.WriteLine(menu);
58             Console.Read();
59         }
60     }
61 }
View Code

 結構是值類型,類是引用類型:

結構使用的成員一般是數值,比如點、矩陣和顏色等輕量對象。值類型在堆棧上分配地址,引用類型在堆上分配地址。堆棧的執行效率要比堆的執行效率高,因此結構的效率高於類。原因在於,堆用完以后由.NET的垃圾收集器自動回收,程序中大量使用堆,將會導致程序性能下降。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Struct
 7 {
 8     struct MyStruct
 9     {
10         // 定義字段x,y
11         public int x;
12         public int y;
13 
14         // 定義構造函數
15         public MyStruct(int i, int j)
16         {
17             x = i;
18             y = j;
19         }
20         // 定義方法
21         public void Sum()
22         {
23             int sum = x + y;
24             Console.WriteLine("The sum is {0}", sum);
25         }
26     }
27     class Program
28     {
29         static void Main(string[] args)
30         {
31             MyStruct s1 = new MyStruct(1, 2);
32             MyStruct s2 = s1 ;
33             s1 .x = 2;
34             s1.Sum ();  // the sum is 4
35             s2 .Sum ();  //the sum is 3
36 
37             MyStruct s3 = new MyStruct();
38             s3 .Sum ();  //  0
39         }
40     }
41 }
View Code

 迭代:

 1 using System;
 2 using System.Collections;  //集合類的命名空間
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace CustomCollection
 8 {
 9     // 定義集合中的元素MyClass類
10     class MyClass
11     {
12         public string Name;
13         public int Age;
14         public MyClass(string name, int age) //代參構造函數
15         {
16             this.Name = name ;
17             this.Age = age;
18  
19         }
20     }
21     public class Iterator : IEnumerator, IEnumerable // 實現接口IEnumerator和IEnumerable類Iterator
22     {
23         // 初始化MyClass類型的集合
24         private MyClass[] ClassArray;
25         int Cnt;
26         public Iterator()
27         {
28             // 使用帶參構造器賦值
29             ClassArray = new MyClass[4];
30             ClassArray[0] = new MyClass("Kith", 23);
31             ClassArray[1] = new MyClass("Smith",30);
32             ClassArray[2] = new MyClass("Geo", 19);
33             ClassArray[3] = new MyClass("Greg", 14);
34             Cnt = -1;
35         }
36         //實現IEnumerator的Reset()方法
37         public void Reset()
38         {
39             //指向第一個元素之前,Cnt為-1,遍歷是從0開始
40             Cnt = -1;
41         }
42         //實現IEnumerator的MoveNext()方法
43         public bool MoveNext()
44         {
45             return (++Cnt < ClassArray.Length);
46         }
47         //實現IEnumerator的Current屬性
48         public object Current
49         {
50             get
51             {
52                 return ClassArray[Cnt];
53             }
54         }
55         // 實現IEnumerable的GetEnumerator()方法
56         public IEnumerator GetEnumerator()
57         {
58             return (IEnumerator)this;
59         }
60     //}
61    // class Program
62    // {
63         static void Main(string[] args)
64         {
65             Iterator It = new Iterator();
66             // 像數組一樣遍歷集合
67             foreach (MyClass MY in It)
68             {
69                 Console.WriteLine("Name:" + MY.Name.ToString());
70                 Console.WriteLine("Age:" + MY.Age.ToString());
71 
72             }
73             Console.Read();
74         }
75     }
76 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM