C#常用泛型使用方法举例

  1 class Program
  2     {
  3         #region Person class for testing
  4         public class Person
  5         {
  6             public int Age { get; set; }
  7             public string FirstName { get; set; }
  8             public string LastName { get; set; }
  9 
 10             public Person()
 11             {
 12  
 13             }
 14 
 15             public Person(string firstName, string lastName, int age)
 16             {
 17                 Age = age;
 18                 FirstName = firstName;
 19                 LastName = lastName;
 20             }
 21 
 22             public override string ToString()
 23             {
 24                 return string.Format("Name:{0} {1},Age:{2}", FirstName, LastName, Age);
 25             }
 26         }
 27         #endregion
 28 
 29         static void Main(string[] args)
 30         {
 31             UseGenericList();
 32             Console.WriteLine("---------------------");
 33 
 34             UseGenericStack();
 35             Console.WriteLine("---------------------");
 36 
 37             UseGenericQueue();
 38             Console.WriteLine("---------------------");
 39 
 40             UseSortedSet();
 41             Console.ReadLine();
 42         }
 43 
 44         #region SortPeopleByAge class
 45         class SortPeopleByAge : IComparer<Person>
 46         {
 47             public int Compare(Person firstPerson, Person secondPerson)
 48             {
 49                 if (firstPerson.Age > secondPerson.Age)
 50                     return 1;
 51                 if (firstPerson.Age < secondPerson.Age)
 52                     return -1;
 53                 else
 54                     return 0;
 55             }
 56         }
 57         #endregion
 58 
 59         #region Ues List<T>
 60         private static void UseGenericList()
 61         {
 62             List<Person> people = new List<Person>()
 63             {
 64                 new Person {FirstName="Zhang",LastName="Nan",Age=23},
 65                 new Person {FirstName= "Homer", LastName="Simpson", Age=47},
 66                 new Person {FirstName= "Marge", LastName="Simpson", Age=45},
 67                 new Person {FirstName= "Lisa", LastName="Simpson", Age=9},
 68                 new Person {FirstName= "Bart", LastName="Simpson", Age=8},
 69             };
 70 
 71             Console.WriteLine("Item in list: {0}", people.Count);
 72 
 73             foreach (Person p in people)
 74                 Console.WriteLine(p);
 75 
 76             Console.WriteLine("\n->Inserting new person");
 77             people.Insert(2, new Person { FirstName = "Test", LastName = "Test", Age = 123});
 78             Console.WriteLine("Items in list:{0}", people.Count);
 79 
 80             Person[] arrayOfPeople = people.ToArray();
 81             for (int i = 0; i < arrayOfPeople.Length; i++)
 82             {
 83                 Console.WriteLine("First Name: {0}", arrayOfPeople[i].FirstName);
 84             }
 85         }
 86         #endregion
 87 
 88         #region Use Stack<T>
 89         static void UseGenericStack()
 90         {
 91             Stack<Person> stackOfPeople = new Stack<Person>();
 92             stackOfPeople.Push(new Person { FirstName = "Zhang", LastName = "Nan", Age = 23 });
 93             stackOfPeople.Push(new Person { FirstName = "Homer", LastName = "Simpson", Age = 47 });
 94             //stackOfPeople.Push(new Person { FirstName = "Marge", LastName = "Simpson", Age = 45 });
 95             stackOfPeople.Push(new Person { FirstName = "Lisa", LastName = "Simpson", Age = 9 });
 96 
 97             Console.WriteLine("First person is : {0}", stackOfPeople.Peek());
 98             Console.WriteLine("Popped off {0}", stackOfPeople.Pop());
 99 
100             Console.WriteLine("\nFirst person is: {0}", stackOfPeople.Peek());
101             Console.WriteLine("Popped off {0}", stackOfPeople.Pop());
102 
103             Console.WriteLine("\nFirst person is: {0}", stackOfPeople.Peek());
104             Console.WriteLine("Popped off {0}", stackOfPeople.Pop());
105 
106             try
107             {
108                 Console.WriteLine("\nFirst person is: {0}", stackOfPeople.Peek());
109                 Console.WriteLine("Popped off {0}", stackOfPeople.Pop());
110             }
111             catch(InvalidOperationException ex)
112             {
113                 Console.WriteLine("\nError! {0}", ex.Message);
114             }
115         }
116         #endregion
117 
118         #region Use Queue<T>
119         static void GetCoffee(Person p)
120         {
121             Console.WriteLine("{0} got coffee!", p.FirstName);
122         }
123 
124         static void UseGenericQueue()
125         {
126             Queue<Person> peopleQ = new Queue<Person>();
127             peopleQ.Enqueue(new Person { FirstName = "Zhang", LastName = "Nan", Age = 23 });
128             peopleQ.Enqueue(new Person { FirstName = "Homer", LastName = "Simpson", Age = 47 });
129             //peopleQ.Enqueue(new Person { FirstName = "Marge", LastName = "Simpson", Age = 45 });
130             peopleQ.Enqueue(new Person { FirstName = "Lisa", LastName = "Simpson", Age = 9 });
131 
132             Console.WriteLine("{0} is first in line!", peopleQ.Peek().FirstName);
133 
134             GetCoffee(peopleQ.Dequeue());
135             GetCoffee(peopleQ.Dequeue());
136             GetCoffee(peopleQ.Dequeue());
137 
138             try
139             {
140                 GetCoffee(peopleQ.Dequeue());
141             }
142             catch (InvalidOperationException e)
143             {
144                 Console.WriteLine("Error! {0}", e.Message);
145             }
146 
147         }
148         #endregion
149 
150         #region Use SortedSet<T>
151         private static void UseSortedSet()
152         {
153             SortedSet<Person> setOfPeople = new SortedSet<Person>(new SortPeopleByAge())
154             {
155                 new Person {FirstName= "Homer", LastName="Simpson", Age=47},
156                 new Person {FirstName= "Marge", LastName="Simpson", Age=45},
157                 new Person {FirstName= "Lisa", LastName="Simpson", Age=9},
158                 new Person {FirstName= "Bart", LastName="Simpson", Age=8}
159             };
160 
161             foreach (Person p in setOfPeople)
162             {
163                 Console.WriteLine(p);
164             }
165 
166             Console.WriteLine();
167 
168             setOfPeople.Add(new Person { FirstName = "Zhang", LastName = "Nan", Age = 23 });
169             setOfPeople.Add(new Person { FirstName = "Abc", LastName = "Test", Age = 120 });
170 
171             foreach (Person p in setOfPeople)
172             {
173                 Console.WriteLine(p);
174             }
175         }
176         #endregion
177     }

注:引用自 《Por C# 2010 and the .NET 4 Platform》

原文地址:https://www.cnblogs.com/zhnhelloworld/p/3044403.html