隨著看的文章及探討越多,越發現自己實在太不定性了,看下《C#深入理解》吧,做一下讀書筆記,從中發現自己的不足。閒話不說,進入正題吧。
在C#1中定下一個簡單的數據類型
1 public class Product1
2 {
3 private string name;
4 private long price;
5
6 public string Name { get { return name; } }
7 public long Price { get { return price; } }
8
9 public Product1(string n, long p)
10 {
11 this.name = n;
12 this.price = p;
13 }
14
15 public static ArrayList GetProducts()
16 {
17 ArrayList list = new ArrayList();
18 list.Add(new Product1("cat", 1));
19 list.Add(new Product1("fish", 2));
20 list.Add(new Product1("dog", 3));
21 list.Add(new Product1("pig", 4));
22 return list;
23 }
24 }
C#1中沒有泛型的概念,在Product1類中的靜態方法返回是的ArrayList類型,裡面的元素類型當然是Product1。而在C#2中引入泛型後,該類可定義為(重命名為Product2)
1 public class Product2
2 {
3 private string name;
4 private long price;
5
6 public string Name { get { return name; } private set { name = value; } }
7 public long Price { get { return price; } private set { price = value; } }
8
9 public Product2(string n, long p)
10 {
11 Name = n;
12 Price = p;
13 }
14
15 public static List<Product2> GetProducts()
16 {
17 List<Product2> list = new List<Product2>();
18 list.Add(new Product2("cat", 1));
19 list.Add(new Product2("fish", 2));
20 list.Add(new Product2("dog", 3));
21 list.Add(new Product2("pig", 4));
22 return list;
23 }
24 }
相比較於C#3,對於屬性的改進,則是引入自動實現屬性的概念,Product1和Product2中私有屬性name和price,可以通過自動屬性進行書寫,如下
1 class Product3
2 {
3 public string Name { get; private set; }
4 public long Price { get; private set; }
5
6 public Product3(string n, long p)
7 {
8 Name = n;
9 Price = p;
10 }
11
12 public static List<Product3> GetProducts()
13 {
14 List<Product3> list = new List<Product3> {
15 new Product3("cat",1),
16 new Product3("fish",2),
17 new Product3("dog",3),
18 new Product3("pig",4)
19 };
20 return list;
21 }
22 }
C#4中對類的實現主要體現在類的實例化中,引入命名實參,注意下面GetProducts方法中類對象的實例化
1 class Product4
2 {
3 readonly string name;
4 readonly long price;
5 public string Name { get { return name; } }
6 public long Price { get { return price; } }
7
8 public Product4(string n, long p)
9 {
10 name = n;
11 price = p;
12 }
13
14 public static List<Product4> GetProducts()
15 {
16 return new List<Product4>
17 {
18 new Product4(n:"cat",p:1),
19 new Product4(n:"fish",p:2),
20 new Product4(n:"dog",p:3),
21 new Product4(n:"pig",p:4)
22 };
23 }
24 }
如new Product4(n:"cat",p:1),格式如[參數:參數值],在實例化中可以顯示的指定參數的值。
接下來說下C#進化過程中對排序方法的實現
在C#1中,需要定義一個實現於IComparer接口的類
1 //使用IComparer對ArrayList進行排序
2 public class ComparerName1 : IComparer
3 {
4 public int Compare(object x, object y)
5 {
6 Product1 p1 = (Product1)x;
7 Product1 p2 = (Product1)y;
8 return p1.Name.CompareTo(p2.Name);
9 }
10 }
在功能頁面要對上述類實例化
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 ArrayList list = Product1.GetProducts();
6
7 list.Sort(new ComparerName1());
8
9 foreach (Product1 p in list)
10 {
11 Console.WriteLine(p.Name);
12 }
13 Console.ReadKey();
14
15 }
16 }
可以看出ArrayList類型有一個公開的接口(用於排序Sort),傳入的參數是實現IComparer接口的一個實例,正好我們上面定義的ComparerName1(根據產品的名字來排序),那麼在C#2中又是如何實現?
正如前面所說C#2引入泛型的概念(對應的產品類為Product2類),定義一個實現IComparer<Product2>接口的類即可
1 public class ComparerName2 : IComparer<Product2>
2 {
3 public int Compare(Product2 x, Product2 y)
4 {
5 return x.Name.CompareTo(y.Name);
6 }
7 }
在功能頁面使用方法和C#1一樣,主要區別在於ComparerName1中需要將Object類型強制轉換成Product1類型,而在使用泛型的情況下,因為已經知道了具體的類型,則避免了強制轉換帶來的性能損耗
。
C#3中的自動屬性對於排序沒有作用,但是可以使用引入的Lambda表達式對排序代碼的進一步精簡。
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 //在C#3中使用Lambda表達式進行排序
6 List<Product3> list = Product3.GetProducts();
7
8 list.Sort(
9 (x, y) => x.Name.CompareTo(y.Name)
10 );
11
12 foreach (Product3 p in list)
13 {
14 Console.WriteLine(p.Name);
15 }
16 Console.ReadKey();
17 }
18 }
Lambda表達式的實現其實是委托,用於委托實現也是一樣的。
下面來說下對於查詢、打印的實現。
C#1
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 ArrayList list = Product1.GetProducts();
6 /*
7 C#1使用查詢、測試、打印
8 */
9 foreach (Product1 p in list)
10 {
11 if (p.Price > 2)
12 {
13 Console.WriteLine(p.Name);
14 }
15 }
16 Console.ReadKey();
17
18 }
19 }
C#2中的查詢實現可以使用委托
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 List<Product2> list = Product2.GetProducts();
6
7 //C#2使用匿名方法創建委托
8
9 Predicate<Product2> test = delegate (Product2 p) { return p.Price > 2; };
10 List<Product2> matches = list.FindAll(test);
11 Action<Product2> print = delegate (Product2 p) { Console.WriteLine(p.Name); }; ;
12 matches.ForEach(print);
13 list.FindAll(test).ForEach(print);
14
15 Console.ReadKey();
16
17 }
18 }
結果和C#1中是一樣的,打印價格大於2產品的名稱,到了C#3則更精簡了,因為有了Lambda表達式
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 List<Product3> list = Product3.GetProducts();
6
7 //C#3中使用Lambda表達式進行查詢
8 list.FindAll(x => x.Price > 2).ForEach(x => Console.WriteLine(x.Name));
9
10 Console.ReadKey();
11
12 }
13 }
寫到這裡,我們有理由相信,Lambda表達式就是變相的委托,則可以引入一個想法,在使用委托的時候均可以使用Lambda表達式替代。great!!!
請斧正。