枚举 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

 

posted @ 2017-10-20 17:20  梓夙  Views(4282)  Comments(0Edit  收藏  举报