public void Test_Generic(string[] commands, int[] args, bool?[] expected) { Assert.AreEqual(commands.Length, args.Length); Assert.AreEqual(commands.Length, expected.Length); bool?[] res = new bool?[commands.Length]; var sol = new MyHashSet(); for (int i = 0; i < commands.Length; i++) { switch (commands[i]) { case "add": sol.Add(args[i]); break; case "remove": sol.Remove(args[i]); break; case "contains": res[i] = sol.Contains(args[i]); break; } } CollectionAssert.AreEqual(res, expected); }
static void Main(string[] args) { MyHashSet hashSet = new MyHashSet(); hashSet.Add(1); hashSet.Add(2); hashSet.Contains(1); // 返回 true hashSet.Contains(3); // 返回 false (未找到) hashSet.Add(2); hashSet.Contains(2); // 返回 true hashSet.Remove(2); hashSet.Contains(2); // 返回 false (已经被删除) }