|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
4 | 4 |
|
5 |
| -class Example |
| 5 | +static class Example |
6 | 6 | {
|
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(); |
15 | 10 |
|
16 |
| - var blueBox = new Box(4, 3, 4); |
17 |
| - AddBox(boxes, blueBox, "blue"); |
| 11 | + Dictionary<Box, string> boxes = new(comparer); |
18 | 12 |
|
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"); |
22 | 16 |
|
23 |
| - Console.WriteLine("The dictionary contains {0} Box objects.", |
24 |
| - boxes.Count); |
25 |
| - } |
| 17 | + Console.WriteLine($"The dictionary contains {boxes.Count} Box objects."); |
26 | 18 |
|
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 | + } |
36 | 31 | }
|
37 | 32 |
|
38 |
| -public class Box |
| 33 | +class Box |
39 | 34 | {
|
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; } |
50 | 38 |
|
51 |
| - public override String ToString() |
| 39 | + public Box(int height, int length, int width) |
52 | 40 | {
|
53 |
| - return String.Format("({0}, {1}, {2})", Height, Length, Width); |
| 41 | + Height = height; |
| 42 | + Length = length; |
| 43 | + Width = width; |
54 | 44 | }
|
| 45 | + |
| 46 | + public override string ToString() => $"({Height}, {Length}, {Width})"; |
55 | 47 | }
|
56 | 48 |
|
57 | 49 | class BoxEqualityComparer : IEqualityComparer<Box>
|
58 | 50 | {
|
59 |
| - public bool Equals(Box b1, Box b2) |
| 51 | + public bool Equals(Box? b1, Box? b2) |
60 | 52 | {
|
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)) |
67 | 54 | return true;
|
68 |
| - else |
| 55 | + |
| 56 | + if (b2 is null || b1 is null) |
69 | 57 | return false;
|
70 |
| - } |
71 | 58 |
|
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; |
76 | 62 | }
|
| 63 | + |
| 64 | + public int GetHashCode(Box box) => box.Height ^ box.Length ^ box.Width; |
77 | 65 | }
|
| 66 | + |
78 | 67 | // The example displays the following output:
|
79 | 68 | // Unable to add (4, 3, 4): An item with the same key has already been added.
|
80 |
| -// |
81 | 69 | // The dictionary contains 2 Box objects.
|
82 | 70 | // </Snippet1>
|
0 commit comments