第十二章 泛型
- 空接合运算符(??) : op1 ?? op2 === op1 == null ? op2 : op1 ( tip:string.empty<>null)
- List<T>:
List
1 namespace 泛型 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 List<MyClass> list = new List<MyClass>(); 8 list.Add(new MyClass("a")); 9 list.Add(new MyClass("b")); 10 11 foreach (var item in list) 12 { 13 item.ToString(); 14 } 15 16 Console.ReadKey(); 17 } 18 } 19 20 public class MyClass 21 { 22 public string Str; 23 24 public MyClass(string str) 25 { 26 this.Str = str; 27 } 28 29 public new void ToString() 30 { 31 Console.WriteLine(this.Str); 32 } 33 } 34 }
- 排序:
Sort
1 namespace Bazinga 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 List<Nerd> nerds = new List<Nerd>(); 8 nerds.Add(new Nerd("Sheldon", 187)); 9 nerds.Add(new Nerd("Leonard", 173)); 10 nerds.Add(new Nerd("Howard", 168)); 11 nerds.Add(new Nerd("Rajesh", 170)); 12 13 nerds.Sort(); 14 15 Console.WriteLine("默认按智商排序:"); 16 foreach (var nerd in nerds) 17 { 18 Console.WriteLine(nerd.ToString()); 19 } 20 21 nerds.Sort(new NerdsSortByName()); 22 Console.WriteLine("\n按名字排序:"); 23 foreach (var nerd in nerds) 24 { 25 Console.WriteLine(nerd.ToString()); 26 } 27 28 Console.ReadKey(); 29 } 30 } 31 32 public class Nerd : IComparable<Nerd> 33 { 34 public string Name { get; set; } 35 public int IQ { get; set; } 36 37 public Nerd(string name, int iq) 38 { 39 this.Name = name; 40 this.IQ = iq; 41 } 42 43 public int CompareTo(Nerd other) 44 { 45 return other.IQ - this.IQ; 46 } 47 48 public override string ToString() 49 { 50 return string.Format("{0},IQ:{1}", this.Name, this.IQ); 51 } 52 } 53 54 public class NerdsSortByName : IComparer<Nerd> 55 { 56 public int Compare(Nerd x, Nerd y) 57 { 58 return x.Name.CompareTo(y.Name); 59 } 60 } 61 }
- 字典:
Dictionary
1 namespace Dictionary 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Dictionary<string, int> dic = new Dictionary<string, int>(); 8 dic.Add("Sheldon", 187); 9 dic.Add("Leonard", 173); 10 dic.Add("Howard", 168); 11 dic.Add("Rajesh", 170); 12 13 foreach (var item in dic.Keys) 14 { 15 Console.WriteLine(item); 16 } 17 18 foreach (var item in dic.Values) 19 { 20 Console.WriteLine(item); 21 } 22 23 foreach (KeyValuePair<string, int> item in dic) 24 { 25 Console.WriteLine("{0},IQ:{1}", item.Key, item.Value); 26 } 27 Console.ReadKey(); 28 } 29 } 30 }
- 定义泛型类:
定义
1 namespace GenericClass 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 MyGenericClass<string> _string = new MyGenericClass<string>("string"); 8 MyGenericClass<string> _string_null = new MyGenericClass<string>(); 9 MyGenericClass<int> _int = new MyGenericClass<int>(100); 10 MyGenericClass<int> _int_null = new MyGenericClass<int>(); 11 MyGenericClass<object> _object = new MyGenericClass<object>("object"); 12 MyGenericClass<object> _object_null = new MyGenericClass<object>(); 13 14 Console.WriteLine(_string.ToString()); 15 Console.WriteLine(_string_null.ToString()); 16 Console.WriteLine(_int.ToString()); 17 Console.WriteLine(_int_null.ToString()); 18 Console.WriteLine(_object.ToString()); 19 Console.WriteLine(_object_null.ToString()); 20 21 Console.ReadKey(); 22 } 23 } 24 25 public class MyGenericClass<T>//where T : object 26 { 27 public T t { get; set; } 28 public MyGenericClass() 29 { 30 this.t = default(T); 31 } 32 public MyGenericClass(T t) 33 { 34 this.t = t; 35 } 36 public override string ToString() 37 { 38 string str = (t==null) ? "null" : t.ToString(); 39 return typeof(T) + " " + str; 40 } 41 } 42 }

浙公网安备 33010602011771号