01.C#數據類型、排序、過濾(一章1.1-1.2)。本站提示廣大學習愛好者:(01.C#數據類型、排序、過濾(一章1.1-1.2))文章只能為提供參考,不一定能成為您想要的結果。以下是01.C#數據類型、排序、過濾(一章1.1-1.2)正文
參考頁面:
http://www.yuanjiaocheng.net/CSharp/csharp-generic-dictionary.html
http://www.yuanjiaocheng.net/CSharp/csharp-partial-class.html
http://www.yuanjiaocheng.net/CSharp/csharp-Static.html
http://www.yuanjiaocheng.net/CSharp/csharp-Anonymous-method.html
http://www.yuanjiaocheng.net/CSharp/csharp-nullable.html
隨著看的文章及討論越多,越發現自己真實太不定性了,看下《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!!!
請指正。