Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

TryAdd exposure/implementation #16642

Merged
merged 3 commits into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void Clear() { }
public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public virtual void OnDeserialization(object sender) { }
public bool Remove(TKey key) { throw null; }
public bool TryAdd(TKey key, TValue value) { throw null; }
void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Add(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Contains(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { throw null; }
void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.CopyTo(System.Collections.Generic.KeyValuePair<TKey, TValue>[] array, int index) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,28 @@ public void GetValueOrDefault_KeyDoesntExist_ReturnsDefaultValue()
Assert.Equal(default(TValue), dictionary.GetValueOrDefault(key));
Assert.Equal(defaultValue, dictionary.GetValueOrDefault(key, defaultValue));
}

[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Dictionary_Generic_TryAdd(int count)
{
var dictionary = (Dictionary<TKey, TValue>)GenericIDictionaryFactory(count);

TKey key = CreateTKey(seed: count);
TValue value = CreateTValue(seed: count);
dictionary.Remove(key);
int originalCount = dictionary.Count;

Assert.True(dictionary.TryAdd(key, value));
Assert.Equal(originalCount + 1, dictionary.Count);
Assert.Equal(dictionary[key], value);

// After adding the key, make sure that trying to add it again fails.
originalCount = dictionary.Count;

Assert.False(dictionary.TryAdd(key, default(TValue)));
Assert.Equal(originalCount, dictionary.Count);
Assert.Equal(dictionary[key], value);
}
}
}