[C#] C#泛型使用演示范例 →→→→→进入此内容的聊天室

来自 , 2020-01-27, 写在 C#, 查看 127 次.
URL http://www.code666.cn/view/e234e195
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace Generics_CSharp
  8. {
  9.     // 尖括号中的类型参数 T。
  10.     public class MyList<T> : IEnumerable<T>
  11.     {
  12.         protected Node head;
  13.         protected Node current = null;
  14.  
  15.         // 嵌套类型也是 T 上的泛型
  16.         protected class Node
  17.         {
  18.             public Node next;
  19.             // T 作为私有成员数据类型。
  20.             private T data;
  21.             // 在非泛型构造函数中使用的 T。
  22.             public Node(T t)
  23.             {
  24.                 next = null;
  25.                 data = t;
  26.             }
  27.             public Node Next
  28.             {
  29.                 get { return next; }
  30.                 set { next = value; }
  31.             }
  32.             // T 作为属性的返回类型。
  33.             public T Data
  34.             {
  35.                 get { return data; }
  36.                 set { data = value; }
  37.             }
  38.         }
  39.  
  40.         public MyList()
  41.         {
  42.             head = null;
  43.         }
  44.  
  45.         // T 作为方法参数类型。
  46.         public void AddHead(T t)
  47.         {
  48.             Node n = new Node(t);
  49.             n.Next = head;
  50.             head = n;
  51.         }
  52.  
  53.         // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
  54.         // foreach 迭代。请注意,在 C# 2.0 中,
  55.         // 不需要实现 Current 和 MoveNext。
  56.         // 编译器将创建实现 IEnumerator<T> 的类。
  57.         public IEnumerator<T> GetEnumerator()
  58.         {
  59.             Node current = head;
  60.  
  61.             while (current != null)
  62.             {
  63.                 yield return current.Data;
  64.                 current = current.Next;
  65.             }
  66.         }
  67.  
  68.         // 必须实现此方法,因为
  69.         // IEnumerable<T> 继承 IEnumerable
  70.         IEnumerator IEnumerable.GetEnumerator()
  71.         {
  72.             return GetEnumerator();
  73.         }
  74.     }
  75.  
  76.  
  77.     public class SortedList<T> : MyList<T> where T : IComparable<T>
  78.     {
  79.         // 一个未优化的简单排序算法,
  80.         // 该算法从低到高对列表元素排序:
  81.         public void BubbleSort()
  82.         {
  83.             if (null == head || null == head.Next)
  84.                 return;
  85.  
  86.             bool swapped;
  87.             do
  88.             {
  89.                 Node previous = null;
  90.                 Node current = head;
  91.                 swapped = false;
  92.  
  93.                 while (current.next != null)
  94.                 {
  95.                     // 由于需要调用此方法,因此,SortedList
  96.                     // 类在 IEnumerable<T> 上是受约束的
  97.                     if (current.Data.CompareTo(current.next.Data) > 0)
  98.                     {
  99.                         Node tmp = current.next;
  100.                         current.next = current.next.next;
  101.                         tmp.next = current;
  102.  
  103.                         if (previous == null)
  104.                         {
  105.                             head = tmp;
  106.                         }
  107.                         else
  108.                         {
  109.                             previous.next = tmp;
  110.                         }
  111.                         previous = tmp;
  112.                         swapped = true;
  113.                     }
  114.  
  115.                     else
  116.                     {
  117.                         previous = current;
  118.                         current = current.next;
  119.                     }
  120.  
  121.                 }// end while
  122.             } while (swapped);
  123.         }
  124.     }
  125.  
  126.     // 一个将自身作为类型参数来实现 IComparable<T> 的简单类,
  127.     // 是对象中的
  128.     // 常用设计模式,这些对象
  129.     // 存储在泛型列表中。
  130.     public class Person : IComparable<Person>
  131.     {
  132.         string name;
  133.         int age;
  134.  
  135.         public Person(string s, int i)
  136.         {
  137.             name = s;
  138.             age = i;
  139.         }
  140.  
  141.         // 这会使列表元素
  142.         // 按 age 值排序。
  143.         public int CompareTo(Person p)
  144.         {
  145.             return age - p.age;
  146.         }
  147.  
  148.         public override string ToString()
  149.         {
  150.             return name + ":" + age;
  151.         }
  152.  
  153.         // 必须实现 Equals。
  154.         public bool Equals(Person p)
  155.         {
  156.             return (this.age == p.age);
  157.         }
  158.     }
  159.  
  160.     class Generics
  161.     {
  162.         static void Main(string[] args)
  163.         {
  164.             // 声明并实例化一个新的范型 SortedList 类。
  165.             // Person 是类型参数。
  166.             SortedList<Person> list = new SortedList<Person>();
  167.  
  168.             // 创建 name 和 age 值以初始化 Person 对象。
  169.             string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
  170.             int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
  171.  
  172.             // 填充列表。
  173.             for (int x = 0; x < names.Length; x++)
  174.             {
  175.                 list.AddHead(new Person(names[x], ages[x]));
  176.             }
  177.  
  178.             Console.WriteLine("Unsorted List:");
  179.             // 打印出未排序的列表。
  180.             foreach (Person p in list)
  181.             {
  182.                 Console.WriteLine(p.ToString());
  183.             }
  184.  
  185.             // 对列表进行排序。
  186.             list.BubbleSort();
  187.  
  188.             Console.WriteLine(String.Format("{0}Sorted List:", Environment.NewLine));
  189.             // 打印出排序的列表。
  190.             foreach (Person p in list)
  191.             {
  192.                 Console.WriteLine(p.ToString());
  193.             }
  194.  
  195.             Console.WriteLine("Done");
  196.         }
  197.     }
  198.  
  199. }
  200.  
  201. //csharp/4914

回复 "C#泛型使用演示范例"

这儿你可以回复上面这条便签

captcha