Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
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
13 changes: 13 additions & 0 deletions src/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public static partial class StructuralComparisons
}
namespace System.Collections.Generic
{
#if netcoreapp11
public static class CollectionExtensions
{
public static TValue GetValueOrDefault<TKey, TValue>(System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key) { throw null; }
public static TValue GetValueOrDefault<TKey, TValue>(System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) { throw null; }
public static TValue GetValueOrDefault<TKey, TValue>(System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) { throw null; }
public static TValue GetValueOrDefault<TKey, TValue>(System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) { throw null; }
}
#endif
public abstract partial class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer
{
protected Comparer() { }
Expand Down Expand Up @@ -80,6 +89,10 @@ public void Clear() { }
public bool ContainsValue(TValue value) { throw null; }
public System.Collections.Generic.Dictionary<TKey, TValue>.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<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Add(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { }
Expand Down
5 changes: 4 additions & 1 deletion src/System.Collections/src/System.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@
<ItemGroup>
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="System\Collections\Generic\CollectionExtensions.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -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<TKey, TValue>(this IDictionary<TKey, TValue> 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<TKey, TValue>(this IDictionary<TKey, TValue> 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<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> 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<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyValuePair<TKey, TValue>>.IsReadOnly
Expand Down