C# 让类型在集合中可被查找
问题
您有一种数据类型,它将存储为 List
解决方法
使用 IComparable
讨论
通过在类(或结构)上实现 IComparable
要实现 CompareTo 方法,请参考这篇文章:
《C# 通过实现IComparer
List
为了使用 List
例(1) 中的 TestSort 方法演示了如何对 List
例 (1) :使类型可查找
public static void TestSearch()
{
List<Square> listOfSquares = new List<Square> {
new Square(1,3),
new Square(4,3),
new Square(2,1),
new Square(6,1)};
IComparer<Square> heightCompare = new CompareHeight();
// Test a List<Square>
Console.WriteLine("List<Square>");
Console.WriteLine("Original list");
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine();
Console.WriteLine("Sorted list using IComparer<Square>=heightCompare");
listOfSquares.Sort(heightCompare);
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine();
Console.WriteLine("Search using IComparer<Square>=heightCompare");
int found = listOfSquares.BinarySearch(new Square(1, 3), heightCompare);
Console.WriteLine($"Found (1,3): {found}");
Console.WriteLine();
Console.WriteLine("Sorted list using IComparable<Square>");
listOfSquares.Sort();
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine("Search using IComparable<Square>");
found = listOfSquares.BinarySearch(new Square(6, 1)); // Use IComparable
Console.WriteLine($"Found (6,1): {found}");
// Test a SortedList<Square>
var sortedListOfSquares = new SortedList<int, Square>(){
{0, new Square(1,3)},
{2, new Square(4,3)},
{1, new Square(2,1)},
{4, new Square(6,1)}};
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("SortedList<Square>");
foreach (KeyValuePair<int, Square> kvp in sortedListOfSquares)
{
Console.WriteLine($"{kvp.Key} : ${kvp.Value}");
}
Console.WriteLine();
bool foundItem = sortedListOfSquares.ContainsKey(2);
Console.WriteLine($"sortedListOfSquares.ContainsKey(2): {foundItem}");
//不使用IComparer或IComparable
//——使用线性搜索和Equals方法
//没有被重载
Square value = new Square(6, 1);
foundItem = sortedListOfSquares.ContainsValue(value);
Console.WriteLine($"sortedListOfSquares.ContainsValue(new Square(6,1)): {foundItem}");
}
这段代码显示的结果如下所示。
List<Square> Original list Height:1 Width:3 Height:4 Width:3 Height:2 Width:1 Height:6 Width:1 Sorted list using IComparer<Square>=heightCompare Height:1 Width:3 Height:2 Width:1 Height:4 Width:3 Height:6 Width:1 Search using IComparer<Square>=heightCompare Found (1,3): 0 Sorted list using IComparable<Square> Height:2 Width:1 Height:1 Width:3 Height:6 Width:1 Height:4 Width:3 Search using IComparable<Square> Found (6,1): 2 SortedList<Square> 0 : Height:1 Width:3 1 : Height:2 Width:1 2 : Height:4 Width:3 4 : Height:6 Width:1 sortedListOfSquares.ContainsKey(2): True sortedListOfSquares.ContainsValue(new Square(6,1)): True
参考
- (1) 《C# 通过实现IComparer
泛型接口比较对象》: https://www.cnblogs.com/netlog/p/16113134.html - (2) 《IComparable
接口 (System)》: https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable-1 - (3) 《IComparer
接口 (System.Collections.Generic)》: https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.icomparer-1?
知乎: @张赐荣
赐荣博客: www.prc.cx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!