static void Main() { try { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary <Box, String> boxes = new Dictionary <Box, string>(boxEqC); Box redBox = new Box(4, 3, 4); Box blueBox = new Box(4, 3, 4); boxes.Add(redBox, "red"); boxes.Add(blueBox, "blue"); } catch (ArgumentException argEx) { Console.WriteLine(argEx.Message); Console.ReadKey(); } }
static void Main() { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); var boxes = new Dictionary <Box, string>(boxEqC); var redBox = new Box(4, 3, 4); AddBox(boxes, redBox, "red"); var blueBox = new Box(4, 3, 4); AddBox(boxes, blueBox, "blue"); var greenBox = new Box(3, 4, 3); AddBox(boxes, greenBox, "green"); Console.WriteLine(); Console.WriteLine("The dictionary contains {0} Box objects.", boxes.Count); }
public void Test_ConcurrentObservableDictionary_With_EqualityComparer() { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); var boxes = new Dictionary <Box, string>(boxEqC); var redBox = new Box(4, 3, 4); boxes.Add(redBox, "red"); var blueBox = new Box(4, 3, 4); Assert.ThrowsException <ArgumentException>( action: () => boxes.Add(blueBox, "blue"), message: "An item with the same key has already been added. Key: (4, 3, 4)"); var greenBox = new Box(3, 4, 3); boxes.Add(greenBox, "green"); Console.WriteLine(); Assert.AreEqual(2, boxes.Count); }