ylbtech-Unitity-CS:Generics
1.A,效果图返回顶部
  1. Unsorted List:
  2. Raul:35
  3. Alessandro:30
  4. Maria:72
  5. Hiroyuki:108
  6. Alok:9
  7. Gunnar:18
  8. Sandra:23
  9. Li:28
  10. Bill:19
  11. Franscoise:45
  12.  
  13. Sorted List:
  14. Alok:9
  15. Gunnar:18
  16. Bill:19
  17. Sandra:23
  18. Li:28
  19. Alessandro:30
  20. Raul:35
  21. Franscoise:45
  22. Maria:72
  23. Hiroyuki:108
  24. Done
  25. 请按任意键继续. . .
1.B,源代码返回顶部
1.B.1,

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

ylbtech-Unitity-CS:Generics的更多相关文章

  1. ylbtech-LanguageSamples-Generics(泛型)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Generics(泛型) 1.A,示例(Sample) 返回顶部 “泛型”示例 (C#) ...

  2. Class:DbConnectionManipulator.cs

    ylbtech-Class:DbConnectionManipulator.cs 1.返回顶部 1.DbConnectionManipulator.cs using System; using Sys ...

  3. ASP.NET MVC:UrlHelper.cs

    ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ...

  4. ASP.NET MVC:WebPageBase.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebPageBase.cs 充当表示 ASP.NET Razor 页的类的基类. 1.A,WebPageBase 抽象类 ...

  5. ASP.NET MVC:WebPageRenderingBase.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebPageRenderingBase.cs 提供用于呈现使用 Razor 视图引擎的页的方法和属性. 1.A,WebP ...

  6. ASP.NET MVC:WebViewPage.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebViewPage.cs 表示呈现使用 ASP.NET Razor 语法的视图所需的属性和方法. 1.A,WebVie ...

  7. Unitity 常用工具类

    ylbtech-Unitity_C#: Unitity 常用代码 1.A,效果图返回顶部   1.B,源代码返回顶部 1,日期字符串 using System; using System.Xml; / ...

  8. Class-SP:Order.cs

    ylbtech-Class-SP:Order.cs 1. 返回顶部 1.GoodsType.cs 货品类别 using System; using System.Collections.Generic ...

  9. System.Net.FtpWebRequest.cs

    ylbtech-System.Net.FtpWebRequest.cs 实现文件传输协议(FTP)客户端. 1.返回顶部 1. #region 程序集 System, Version=4.0.0.0, ...

随机推荐

  1. Java之JUC系列:外部Tools

    前面写了两篇JDBC源码的文章,自己都觉得有点枯燥,先插一段JUC系列的文章来换换胃口,前面有文章大概介绍过JUC包含的东西,JUC体系包含的内容也是非常的多,不是一两句可以说清楚的,我这首先列出将会 ...

  2. dir:一行代码,提取出所有视频文件名称及路径

    某次,部门接到一个任务,要求对公司现有的视频文件资料做一个统计整理分类的工作. 领导召集开会,问:两周时间够用吗? 统计整理分类工作的第一步骤是把视频文件名称来源类别信息录入到 excel 表格中,才 ...

  3. Xcode 7 ImageNamed 方法加载jpg图片失败

    更新XCode7后 原来的Image.xcassets文件夹变成了Assets.xcassets 把01.jpg,02.jpg,03.png拖入这个文件夹中 UIImage* test1=[UIIma ...

  4. System.Diagnostics命名空间里的Debug类和Trace类的用途

    在 .NET 类库中有一个 System.Diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类--Debug ...

  5. 010. 使用.net框架提供的属性

    C#允许在类和类成员上声明特性(类), 可在运行时解释类和类的成员. 这个特性也称为属性, 使用Attribute.下面演示如何使用.net框架提供的属性. using System; using S ...

  6. 将Xml或Json生成类的最简单方式

      来源于 http://stackoverflow.com/questions/3187444/convert-xml-string-to-object

  7. #linux包之tcpdump之tcpdump命令

    概述 man tcpdump 已阅 yum install tcpdump Downloading Packages:(1/2): libpcap-1.4.0-1.20130826git2dbcaa1 ...

  8. unity, particleSystem的batch优化

    一,单个光效的batch优化 理想状态下一个由若干粒子堆出来的光效只需要一至两个draw call: (1)至多使用alpha blend(垫底色)和additive(曝光)两个材质球,两shader ...

  9. C# toString()转换详细(转)

    文章转自:http://blog.csdn.net/xiaoguang44/article/details/6988418 字符型转换为字符串 // C 货币 2.5.ToString("C ...

  10. Kindle 实用技巧

    1.Kindle Mate:可以连接kindle导出生词本,笔记,注释,超级棒. 2.把PDF发送到Kindle的时候,邮件主题写convert,这样系统会自动把PDF转换成适合Kindle阅读的格式 ...