.NET 开发基金会/使用泛型集合
外观
| .NET 开发基金会 | |
|---|---|
系统类型和集合:使用泛型集合
考试目标:通过使用泛型集合,提高 .NET Framework 应用程序的类型安全性和应用程序性能。
(参考 System.Collections.Generic 命名空间 MSDN )
- 泛型 IComparable 接口 - MSDN
- 请注意,IComparable<T> 是 System 命名空间的成员。
- 当您创建一个类并希望它与支持排序的泛型类型(例如 SortedList<T> 或 List<T>.Sort())一起使用时,您将使用此接口,而无需指定比较器对象。IComparable<T> 的唯一方法是 CompareTo<T>(T other)。MSDN 上有一个示例。
- 以下示例为自定义的 Point 类实现 IComparable<T>。该示例使用 List<T> 而不是 SortedList<T> 或 OrderedDictionnary<T>,因为比较是根据点到原点的距离进行的,对于许多点来说,距离值可能相同。
简单使用 IComparable<T> (C#)
简单使用 IComparable<T> (C#)
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericsLab05
{
class Program
{
static void Main(string[] args)
{
List<Point> lst = new List<Point>();
lst.Add(new Point(-2, -2));
lst.Add(new Point(1, 1));
lst.Add(new Point(2, 2));
// Sort uses IComparable of Point
lst.Sort();
foreach (Point pt in lst)
{
Console.WriteLine(pt.ToString());
}
// Wait to finish
Console.WriteLine("Press ENTER to finish");
Console.ReadLine();
}
}
// This is out custom version of a point
public struct Point : IComparable<Point>
{
public double x;
public double y;
public Point(double px, double py)
{
x = px;
y = py;
}
// Comparaison done based on distance from origin
public int CompareTo(Point other)
{
return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)).CompareTo
(Math.Sqrt(Math.Pow(other.x, 2) + Math.Pow(other.y, 2)));
}
public override string ToString()
{
return "(" + x.ToString() + "," + y.ToString() + ")";
}
}
}
- 泛型 ICollection 接口和泛型 IList 接口
- 泛型 ICollection 接口 - MSDN
- 泛型 IList 接口 - MSDN
- 泛型 IComparer 接口和泛型 IEqualityComparer 接口
- 泛型 IComparer 接口 - MSDN
- 泛型 IEqualityComparer 接口 - MSDN
- 泛型 IDictionary 接口 - MSDN
- 泛型 IEnumerable 接口和泛型 IEnumerator 接口
- 泛型 IEnumerable 接口 - MSDN
- 另请参见 ONDotnet
- 泛型 IEnumerator 接口 - MSDN
- IHashCodeProvider 接口 - MSDN - 该接口现已过时(从 .NET 2.0 开始)
- 泛型字典类和泛型字典.枚举器结构
- 泛型字典类 - MSDN
- 泛型字典.枚举器结构 - MSDN
- 泛型字典.键集合类和字典.键集合.枚举器结构
- 泛型字典.键集合类 - MSDN
- 字典.键集合.枚举器结构 - MSDN
- 泛型字典.值集合类和字典.值集合.枚举器结构
- 泛型字典.值集合类 - MSDN
- 字典.值集合.枚举器结构 - MSDN
- 泛型比较器类 - MSDN
- Comparer<T> 类用作基类,可以轻松地实现 IComparer<T> 接口。
- 该示例与 IComparable<T> 相同,只是现在将派生的 Comparer<T> 对象传递给 List<T>.Sort() 方法,而不是在 Point 上实现 IComparable<T> 接口。
- 这种处理方式有两个优点:
- 即使您无法访问 Point 的源代码,也可以使用它。
- 您可以为同一个 Point 类创建多个派生的比较器类。
- 这种处理方式有两个优点:
自定义 Comparer<T> (C#)
自定义 Comparer<T> (C#)
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericsLab06
{
class Program
{
static void Main(string[] args)
{
List<Point> lst = new List<Point>();
lst.Add(new Point(-2, -2));
lst.Add(new Point(1, 1));
lst.Add(new Point(2, 2));
// Sort uses IComparable of Point
lst.Sort(new DistanceComparer());
foreach (Point pt in lst)
{
Console.WriteLine(pt.ToString());
}
// Wait to finish
Console.WriteLine("Press ENTER to finish");
Console.ReadLine();
}
}
// This is out custom version of a point
public struct Point
{
public double x;
public double y;
public Point(double px, double py)
{
x = px;
y = py;
}
public override string ToString()
{
return "(" + x.ToString() + "," + y.ToString() + ")";
}
}
// Derive from base comparer class to implement IComparer<T>
public class DistanceComparer : Comparer<Point>
{
public override int Compare(Point p1, Point p2)
{
return Math.Sqrt(Math.Pow(p1.x, 2) + Math.Pow(p1.y, 2)).CompareTo
(Math.Sqrt(Math.Pow(p2.x, 2) + Math.Pow(p2.y, 2)));
}
}
}
- 泛型相等比较器类 - MSDN
- 参见 MSDN
- 泛型列表类 - MSDN
- 泛型列表类实例只需使用 List<T> 语法声明,其中 T 是特定类型。
- 泛型列表.枚举器结构 - MSDN
- 泛型排序列表类 - MSDN
- 泛型队列类 - MSDN
- 泛型队列.枚举器结构 - MSDN
- 参见 MSDN
- 有关排序列表和排序字典之间差异的解释,请参见 MSDN
- 泛型链表表示一个双向链表,是一个通用的链表。它支持枚举器并实现 ICollection 接口,与 .NET Framework 中的其他类一致。
- 泛型链表类 - MSDN
- 泛型链表.枚举器结构 - MSDN
- 泛型链表节点类 - MSDN
- 泛型栈类 - MSDN
- 泛型栈.枚举器结构 - MSDN