Skip to content

Commit 107aa62

Browse files
authored
Modernize custom equality comparer sample code (dotnet#9092)
* Modernize custom equality comparer sample code Brings this sample up to a more modern C# standard, and makes the formatting consistent. * Update program.cs
1 parent 10597c3 commit 107aa62

File tree

1 file changed

+41
-53
lines changed
  • snippets/csharp/System.Collections.Generic/IEqualityComparerT/Overview

1 file changed

+41
-53
lines changed

snippets/csharp/System.Collections.Generic/IEqualityComparerT/Overview/program.cs

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,69 @@
22
using System;
33
using System.Collections.Generic;
44

5-
class Example
5+
static class Example
66
{
7-
static void Main()
8-
{
9-
BoxEqualityComparer boxEqC = new BoxEqualityComparer();
10-
11-
var boxes = new Dictionary<Box, string>(boxEqC);
12-
13-
var redBox = new Box(4, 3, 4);
14-
AddBox(boxes, redBox, "red");
7+
static void Main()
8+
{
9+
BoxEqualityComparer comparer = new();
1510

16-
var blueBox = new Box(4, 3, 4);
17-
AddBox(boxes, blueBox, "blue");
11+
Dictionary<Box, string> boxes = new(comparer);
1812

19-
var greenBox = new Box(3, 4, 3);
20-
AddBox(boxes, greenBox, "green");
21-
Console.WriteLine();
13+
AddBox(new Box(4, 3, 4), "red");
14+
AddBox(new Box(4, 3, 4), "blue");
15+
AddBox(new Box(3, 4, 3), "green");
2216

23-
Console.WriteLine("The dictionary contains {0} Box objects.",
24-
boxes.Count);
25-
}
17+
Console.WriteLine($"The dictionary contains {boxes.Count} Box objects.");
2618

27-
private static void AddBox(Dictionary<Box, String> dict, Box box, String name)
28-
{
29-
try {
30-
dict.Add(box, name);
31-
}
32-
catch (ArgumentException e) {
33-
Console.WriteLine("Unable to add {0}: {1}", box, e.Message);
34-
}
35-
}
19+
void AddBox(Box box, string name)
20+
{
21+
try
22+
{
23+
boxes.Add(box, name);
24+
}
25+
catch (ArgumentException e)
26+
{
27+
Console.WriteLine($"Unable to add {box}: {e.Message}");
28+
}
29+
}
30+
}
3631
}
3732

38-
public class Box
33+
class Box
3934
{
40-
public Box(int h, int l, int w)
41-
{
42-
this.Height = h;
43-
this.Length = l;
44-
this.Width = w;
45-
}
46-
47-
public int Height { get; set; }
48-
public int Length { get; set; }
49-
public int Width { get; set; }
35+
public int Height { get; }
36+
public int Length { get; }
37+
public int Width { get; }
5038

51-
public override String ToString()
39+
public Box(int height, int length, int width)
5240
{
53-
return String.Format("({0}, {1}, {2})", Height, Length, Width);
41+
Height = height;
42+
Length = length;
43+
Width = width;
5444
}
45+
46+
public override string ToString() => $"({Height}, {Length}, {Width})";
5547
}
5648

5749
class BoxEqualityComparer : IEqualityComparer<Box>
5850
{
59-
public bool Equals(Box b1, Box b2)
51+
public bool Equals(Box? b1, Box? b2)
6052
{
61-
if (b2 == null && b1 == null)
62-
return true;
63-
else if (b1 == null || b2 == null)
64-
return false;
65-
else if(b1.Height == b2.Height && b1.Length == b2.Length
66-
&& b1.Width == b2.Width)
53+
if (ReferenceEquals(b1, b2))
6754
return true;
68-
else
55+
56+
if (b2 is null || b1 is null)
6957
return false;
70-
}
7158

72-
public int GetHashCode(Box bx)
73-
{
74-
int hCode = bx.Height ^ bx.Length ^ bx.Width;
75-
return hCode.GetHashCode();
59+
return b1.Height == b2.Height
60+
&& b1.Length == b2.Length
61+
&& b1.Width == b2.Width;
7662
}
63+
64+
public int GetHashCode(Box box) => box.Height ^ box.Length ^ box.Width;
7765
}
66+
7867
// The example displays the following output:
7968
// Unable to add (4, 3, 4): An item with the same key has already been added.
80-
//
8169
// The dictionary contains 2 Box objects.
8270
// </Snippet1>

0 commit comments

Comments
 (0)