public void When_creating_a_new_keyedCollectionEx_with_non_default_comparer() { var personA = new Person { Name = "A", Age = 1 }; var personB = new Person { Name = "B", Age = 2 }; var personC = new Person { Name = "C", Age = 3 }; var collection = new KeyedCollectionEx <string, Person>(p => p.Name, StringComparer.OrdinalIgnoreCase) { personA, personB }; collection.ShouldNotBeNull(); collection.Count.ShouldBe(2); collection.Contains("A").ShouldBeTrue(); collection.Contains("a").ShouldBeTrue(); collection.Contains("B").ShouldBeTrue(); collection.Contains("b").ShouldBeTrue(); collection.Contains("C").ShouldBeFalse(); collection.Contains("c").ShouldBeFalse(); collection.Contains(personA).ShouldBeTrue(); collection.Contains(personB).ShouldBeTrue(); collection.Contains(personC).ShouldBeFalse(); collection.Add(personC); collection.Contains("C").ShouldBeTrue(); collection.Contains("c").ShouldBeTrue(); collection.Contains(personC).ShouldBeTrue(); var dodgyPerson = new Person { Name = "A", Age = 666 }; Should.Throw <ArgumentException>(() => collection.Add(dodgyPerson)).Message .ShouldBe("An item with the same key has already been added."); collection["A"].Age.ShouldBe(1); collection["B"].Age.ShouldBe(2); collection["C"].Age.ShouldBe(3); Person somePerson; collection.TryGet("A", out somePerson).ShouldBeTrue(); somePerson.ShouldNotBeNull(); somePerson.Age.ShouldBe(1); collection.TryGet("D", out somePerson).ShouldBeFalse(); somePerson.ShouldBeNull(); }
/// <summary> /// Adds the <paramref name="key"/> and <paramref name="value"/> to the <paramref name="keyedCollection"/> /// if the <paramref name="key"/> does not already exists and returns the inserted value. /// </summary> public static TValue GetOrAdd <TKey, TValue>(this KeyedCollectionEx <TKey, TValue> keyedCollection, TKey key, TValue value) { if (!keyedCollection.TryGet(key, out TValue result)) { keyedCollection.Add(value); return(value); } return(result); }
/// <summary> /// Adds the <paramref name="key"/> and the value created by <paramref name="valueCreator"/> to /// the <paramref name="keyedCollection"/> if the <paramref name="key"/> does not already exists /// and returns the inserted value. /// </summary> public static TValue GetOrAdd <TKey, TValue>(this KeyedCollectionEx <TKey, TValue> keyedCollection, TKey key, Func <TValue> valueCreator) { if (!keyedCollection.TryGet(key, out TValue result)) { var value = valueCreator(); keyedCollection.Add(value); result = value; } return(result); }