diff --git a/src/System.Collections/ref/System.Collections.cs b/src/System.Collections/ref/System.Collections.cs index 3a68a37e4f7f..56ef35c4bbf8 100644 --- a/src/System.Collections/ref/System.Collections.cs +++ b/src/System.Collections/ref/System.Collections.cs @@ -40,6 +40,15 @@ public static partial class StructuralComparisons } namespace System.Collections.Generic { +#if netcoreapp11 + public static class CollectionExtensions + { + public static TValue GetValueOrDefault(System.Collections.Generic.IDictionary dictionary, TKey key) { throw null; } + public static TValue GetValueOrDefault(System.Collections.Generic.IDictionary dictionary, TKey key, TValue defaultValue) { throw null; } + public static TValue GetValueOrDefault(System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key) { throw null; } + public static TValue GetValueOrDefault(System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key, TValue defaultValue) { throw null; } + } +#endif public abstract partial class Comparer : System.Collections.Generic.IComparer, System.Collections.IComparer { protected Comparer() { } @@ -80,6 +89,10 @@ public void Clear() { } public bool ContainsValue(TValue value) { throw null; } public System.Collections.Generic.Dictionary.Enumerator GetEnumerator() { throw null; } public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } +#if netcoreapp11 + public TValue GetValueOrDefault(TKey key) { throw null; } + public TValue GetValueOrDefault(TKey key, TValue defaultValue) { throw null; } +#endif public virtual void OnDeserialization(object sender) { } public bool Remove(TKey key) { throw null; } void System.Collections.Generic.ICollection>.Add(System.Collections.Generic.KeyValuePair keyValuePair) { } diff --git a/src/System.Collections/src/System.Collections.csproj b/src/System.Collections/src/System.Collections.csproj index 74c95124ed50..3affdd7de58a 100644 --- a/src/System.Collections/src/System.Collections.csproj +++ b/src/System.Collections/src/System.Collections.csproj @@ -86,5 +86,8 @@ + + + - + \ No newline at end of file diff --git a/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs b/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs new file mode 100644 index 000000000000..0d19f9d3ed35 --- /dev/null +++ b/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs @@ -0,0 +1,41 @@ +namespace System.Collections.Generic +{ + public static class CollectionExtensions + { + // Method similar to TryGetValue that returns the value instead of putting it in an out param. + public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key) => dictionary.GetValueOrDefault(key, default(TValue)); + + // Method similar to TryGetValue that returns the value instead of putting it in an out param. If the entry + // doesn't exist, returns the defaultValue instead. + public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue) + { + if (dictionary == null) + { + throw new ArgumentNullException(nameof(dictionary)); + } + + TValue value; + if (dictionary.TryGetValue(key, out value)) + return value; + return defaultValue; + } + + // Method similar to TryGetValue that returns the value instead of putting it in an out param. + public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key) => dictionary.GetValueOrDefault(key, default(TValue)); + + // Method similar to TryGetValue that returns the value instead of putting it in an out param. If the entry + // doesn't exist, returns the defaultValue instead. + public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key, TValue defaultValue) + { + if (dictionary == null) + { + throw new ArgumentNullException(nameof(dictionary)); + } + + TValue value; + if (dictionary.TryGetValue(key, out value)) + return value; + return defaultValue; + } + } +} diff --git a/src/System.Collections/src/System/Collections/Generic/Dictionary.cs b/src/System.Collections/src/System/Collections/Generic/Dictionary.cs index 09d213b2a760..c3ba8f258b45 100644 --- a/src/System.Collections/src/System/Collections/Generic/Dictionary.cs +++ b/src/System.Collections/src/System/Collections/Generic/Dictionary.cs @@ -537,18 +537,19 @@ public bool TryGetValue(TKey key, out TValue value) return false; } - // This is a convenience method for the internal callers that were converted from using Hashtable. - // Many were combining key doesn't exist and key exists but null value (for non-value types) checks. - // This allows them to continue getting that behavior with minimal code delta. This is basically - // TryGetValue without the out param - internal TValue GetValueOrDefault(TKey key) + // Method similar to TryGetValue that returns the value instead of putting it in an out param. + public TValue GetValueOrDefault(TKey key) => GetValueOrDefault(key, default(TValue)); + + // Method similar to TryGetValue that returns the value instead of putting it in an out param. If the entry + // doesn't exist, returns the defaultValue instead. + public TValue GetValueOrDefault(TKey key, TValue defaultValue) { int i = FindEntry(key); if (i >= 0) { return entries[i].value; } - return default(TValue); + return defaultValue; } bool ICollection>.IsReadOnly