.Net 2.0开始支持泛型,下面给出摘录一个MSDN上关于泛型的例子
2006-07-01 09:52
756 查看
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Generics_CSharp
{
// 尖括号中的类型参数 T。
public class MyList<T> : IEnumerable<T>
{
protected Node head;
protected Node current = null;
// 嵌套类型也是 T 上的泛型
protected class Node
{
public Node next;
// T 作为私有成员数据类型。
private T data;
// 在非泛型构造函数中使用的 T。
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
// T 作为属性的返回类型。
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
}
// T 作为方法参数类型。
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
// 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
// foreach 迭代。请注意,在 C# 2.0 中,
// 不需要实现 Current 和 MoveNext。
// 编译器将创建实现 IEnumerator<T> 的类。
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
// 必须实现此方法,因为
// IEnumerable<T> 继承 IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
// where T : IComparable<T> 表示这个T类型必须实现了Icomparable接口
public class SortedList<T> : MyList<T> where T : IComparable<T>
{
// 一个未优化的简单排序算法,
// 该算法从低到高对列表元素排序:
public void BubbleSort()
{
if (null == head || null == head.Next)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
// 由于需要调用此方法,因此,SortedList
// 类在 IEnumerable<T> 上是受约束的
if (current.Data.CompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
head = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}// 结束 while
} while (swapped);
}
}
// 一个将自身作为类型参数来实现 IComparable<T> 的简单类,
// 是对象中的
// 常用设计模式,这些对象
// 存储在泛型列表中。
public class Person : IComparable<Person>
{
string name;
int age;
public Person(string s, int i)
{
name = s;
age = i;
}
// 这会使列表元素
// 按 age 值排序。
public int CompareTo(Person p)
{
return age - p.age;
}
public override string ToString()
{
return name + ":" + age;
}
// 必须实现 Equals。
public bool Equals(Person p)
{
return (this.age == p.age);
}
}
class Generics
{
static void Main(string[] args)
{
// 声明并实例化一个新的范型 SortedList 类。
// Person 是类型参数。
SortedList<Person> list = new SortedList<Person>();
// 创建 name 和 age 值以初始化 Person 对象。
string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
// 填充列表。
for (int x = 0; x < names.Length; x++)
{
list.AddHead(new Person(names[x], ages[x]));
}
Console.WriteLine("Unsorted List:");
// 打印出未排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
}
// 对列表进行排序。
list.BubbleSort();
Console.WriteLine(String.Format("{0}Sorted List:", Environment.NewLine));
// 打印出排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
}
Console.WriteLine("Done");
}
}
}
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Generics_CSharp
{
// 尖括号中的类型参数 T。
public class MyList<T> : IEnumerable<T>
{
protected Node head;
protected Node current = null;
// 嵌套类型也是 T 上的泛型
protected class Node
{
public Node next;
// T 作为私有成员数据类型。
private T data;
// 在非泛型构造函数中使用的 T。
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
// T 作为属性的返回类型。
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
}
// T 作为方法参数类型。
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
// 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
// foreach 迭代。请注意,在 C# 2.0 中,
// 不需要实现 Current 和 MoveNext。
// 编译器将创建实现 IEnumerator<T> 的类。
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
// 必须实现此方法,因为
// IEnumerable<T> 继承 IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
// where T : IComparable<T> 表示这个T类型必须实现了Icomparable接口
public class SortedList<T> : MyList<T> where T : IComparable<T>
{
// 一个未优化的简单排序算法,
// 该算法从低到高对列表元素排序:
public void BubbleSort()
{
if (null == head || null == head.Next)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
// 由于需要调用此方法,因此,SortedList
// 类在 IEnumerable<T> 上是受约束的
if (current.Data.CompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
head = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}// 结束 while
} while (swapped);
}
}
// 一个将自身作为类型参数来实现 IComparable<T> 的简单类,
// 是对象中的
// 常用设计模式,这些对象
// 存储在泛型列表中。
public class Person : IComparable<Person>
{
string name;
int age;
public Person(string s, int i)
{
name = s;
age = i;
}
// 这会使列表元素
// 按 age 值排序。
public int CompareTo(Person p)
{
return age - p.age;
}
public override string ToString()
{
return name + ":" + age;
}
// 必须实现 Equals。
public bool Equals(Person p)
{
return (this.age == p.age);
}
}
class Generics
{
static void Main(string[] args)
{
// 声明并实例化一个新的范型 SortedList 类。
// Person 是类型参数。
SortedList<Person> list = new SortedList<Person>();
// 创建 name 和 age 值以初始化 Person 对象。
string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
// 填充列表。
for (int x = 0; x < names.Length; x++)
{
list.AddHead(new Person(names[x], ages[x]));
}
Console.WriteLine("Unsorted List:");
// 打印出未排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
}
// 对列表进行排序。
list.BubbleSort();
Console.WriteLine(String.Format("{0}Sorted List:", Environment.NewLine));
// 打印出排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
}
Console.WriteLine("Done");
}
}
}
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
相关文章推荐
- 关于C#2.0泛型应用的一个小例子
- 一个关于.net下面的底层设备调用问题
- 下面给出一个$.ajax()方法实现异步请求的例子
- 利用字母可以组成一些美丽的图形,下面给出了一个例子
- msdn 上的关于delegate和event的一个例子,理解后加点评论
- MSDN关于.net 2.0 beta 版本中范型实现的介绍
- 让 NDoc 1.3 支持.NET 2.0 程序集,泛型输出和 Visual studio 2005 解决方案导入
- ZedGraph:一个同时支持WinForm和WebForm的开源图表控件(基于LGPL协议,.NET 2.0 C#源代码)
- stl(三)------这是一个MSDN中的一个关于map 中find函数的说明 很好的一个例子
- 求一个页面浮动层的例子?要求当用户上下拉动IE滚动条的时候,层自动移动,但是开始时该层与浏览器顶端有距离,最后随着移动该层停留在顶端下面不动,当移动到最上面时恢复原来。
- 贴一个MSDN上关于IFormatProvider接口的例子
- 关于java里面Collection包括List,Map等初始化内部匿名类和泛型的一个例子
- .net 2008中在html源码中的《script》可以添加c#后台的源代码。下面是MSDN中的例子
- .net 2.0 以后的版本:代理一个对象的时候,支持继承树
- MSDN中关于结构成员对齐的一个例子
- 下面给出一个$.ajax()方法实现异步请求的例子
- 让 NDoc 1.3 支持.NET 2.0 程序集,泛型输出和 Visual studio 2005 解决方案导入
- 找到一个全能的免费空间!支持SQL和.net2.0
- .net 2.0学习动态语言的另外一个例子?
- 【读书笔记】MSDN 上关于加密解密的一个例子