public void UpdateOrAdd2() { ICollection <String> coll = new HashBag <String>(); // s1 and s2 are distinct objects but contain the same text: String s1 = "abc", s2 = ("def" + s1).Substring(3); Assert.IsFalse(coll.UpdateOrAdd(s1, out string old)); Assert.AreEqual(null, old); Assert.IsTrue(coll.UpdateOrAdd(s2, out old)); Assert.IsTrue(Object.ReferenceEquals(s1, old)); Assert.IsFalse(Object.ReferenceEquals(s2, old)); }