本文整理汇总了C#中System.Collections.Generic.EqualityComparer<T>类的典型用法代码示例。如果您正苦于以下问题:C# EqualityComparer<T>类的具体用法?C# EqualityComparer<T>怎么用?C# EqualityComparer<T>使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EqualityComparer<T>类属于System.Collections.Generic命名空间,在下文中一共展示了EqualityComparer<T>类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
class Program
{
static Dictionary<Box, String> boxes;
static void Main()
{
BoxSameDimensions boxDim = new BoxSameDimensions();
boxes = new Dictionary<Box, string>(boxDim);
Console.WriteLine("Boxes equality by dimensions:");
Box redBox = new Box(8, 4, 8);
Box greenBox = new Box(8, 6, 8);
Box blueBox = new Box(8, 4, 8);
Box yellowBox = new Box(8, 8, 8);
AddBox(redBox, "red");
AddBox(greenBox, "green");
AddBox(blueBox, "blue");
AddBox(yellowBox, "yellow");
Console.WriteLine();
Console.WriteLine("Boxes equality by volume:");
BoxSameVolume boxVolume = new BoxSameVolume();
boxes = new Dictionary<Box, string>(boxVolume);
Box pinkBox = new Box(8, 4, 8);
Box orangeBox = new Box(8, 6, 8);
Box purpleBox = new Box(4, 8, 8);
Box brownBox = new Box(8, 8, 4);
AddBox(pinkBox, "pink");
AddBox(orangeBox, "orange");
AddBox(purpleBox, "purple");
AddBox(brownBox, "brown");
}
public static void AddBox(Box bx, string name)
{
try
{
boxes.Add(bx, name);
Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",
name, boxes.Count.ToString(), bx.GetHashCode());
}
catch (ArgumentException)
{
Console.WriteLine("A box equal to {0} is already in the collection.", name);
}
}
}
public class Box
{
public Box(int h, int l, int w)
{
this.Height = h;
this.Length = l;
this.Width = w;
}
public int Height { get; set; }
public int Length { get; set; }
public int Width { get; set; }
}
class BoxSameDimensions : EqualityComparer<Box>
{
public override bool Equals(Box b1, Box b2)
{
if (b1 == null && b2 == null)
return true;
else if (b1 == null || b2 == null)
return false;
return (b1.Height == b2.Height &&
b1.Length == b2.Length &&
b1.Width == b2.Width);
}
public override int GetHashCode(Box bx)
{
int hCode = bx.Height ^ bx.Length ^ bx.Width;
return hCode.GetHashCode();
}
}
class BoxSameVolume : EqualityComparer<Box>
{
public override bool Equals(Box b1, Box b2)
{
if (b1 == null && b2 == null)
return true;
else if (b1 == null || b2 == null)
return false;
return (b1.Height * b1.Width * b1.Length ==
b2.Height * b2.Width * b2.Length);
}
public override int GetHashCode(Box bx)
{
int hCode = bx.Height * bx.Length * bx.Width;
return hCode.GetHashCode();
}
}
输出:
* Boxes equality by dimensions: Added red, Count = 1, HashCode = 46104728 Added green, Count = 2, HashCode = 12289376 A box equal to blue is already in the collection. Added yellow, Count = 3, HashCode = 43495525 Boxes equality by volume: Added pink, Count = 1, HashCode = 55915408 Added orange, Count = 2, HashCode = 33476626 A box equal to purple is already in the collection. A box equal to brown is already in the collection. *