From 549263e4fabc9b75aa976fd456966a6b26f3aa48 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 12 Dec 2019 10:35:35 -0500 Subject: [PATCH 1/3] Remove TKey : notnull constraint from `IDictionary` The interface explicitly documents that some implementations may choose to support null keys. --- .../src/System/Collections/Generic/IDictionary.cs | 2 +- src/libraries/System.Runtime/ref/System.Runtime.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs index a79782c9d204c1..3e3fa3618cd0f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs @@ -10,7 +10,7 @@ namespace System.Collections.Generic // Keys can be any non-null object. Values can be any object. // You can look up a value in an IDictionary via the default indexed // property, Items. - public interface IDictionary : ICollection> where TKey : notnull + public interface IDictionary : ICollection> { // Interfaces are not serializable // The Item property provides methods to read and edit entries diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index c1ce2529f3303f..75f8de8a5f22f3 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -3954,7 +3954,7 @@ public partial interface IComparer { int Compare([System.Diagnostics.CodeAnalysis.AllowNullAttribute] T x, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] T y); } - public partial interface IDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable where TKey : notnull + public partial interface IDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { TValue this[TKey key] { get; set; } System.Collections.Generic.ICollection Keys { get; } From 6f72ccc5ff369ae90d876212667cf279b1285374 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 12 Dec 2019 14:39:06 -0500 Subject: [PATCH 2/3] Remove TKey : notnull constraint from `IReadOnlyDictionary` --- .../src/System/Collections/Generic/IReadOnlyDictionary.cs | 2 +- src/libraries/System.Runtime/ref/System.Runtime.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IReadOnlyDictionary.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IReadOnlyDictionary.cs index af7ff0871dc1c9..47ec8eae759436 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IReadOnlyDictionary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IReadOnlyDictionary.cs @@ -7,7 +7,7 @@ namespace System.Collections.Generic { // Provides a read-only view of a generic dictionary. - public interface IReadOnlyDictionary : IReadOnlyCollection> where TKey : notnull + public interface IReadOnlyDictionary : IReadOnlyCollection> { bool ContainsKey(TKey key); bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value); diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 75f8de8a5f22f3..b39fd8be029ed3 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -3988,7 +3988,7 @@ public partial interface IReadOnlyCollection : System.Collections.Generic { int Count { get; } } - public partial interface IReadOnlyDictionary : System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.IEnumerable where TKey : notnull + public partial interface IReadOnlyDictionary : System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.IEnumerable { TValue this[TKey key] { get; } System.Collections.Generic.IEnumerable Keys { get; } From 6d1a809736541e0c5356467f9417847551a9ff58 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 12 Dec 2019 18:00:57 -0500 Subject: [PATCH 3/3] Remove notnull constraints from extension methods on those interfaces --- .../System.Collections/ref/System.Collections.cs | 8 ++++---- .../System/Collections/Generic/CollectionExtensions.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Collections/ref/System.Collections.cs b/src/libraries/System.Collections/ref/System.Collections.cs index 85f581fb8ad11c..c10810f6a5c97d 100644 --- a/src/libraries/System.Collections/ref/System.Collections.cs +++ b/src/libraries/System.Collections/ref/System.Collections.cs @@ -45,11 +45,11 @@ namespace System.Collections.Generic public static partial class CollectionExtensions { [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] - public static TValue GetValueOrDefault(this System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key) where TKey : notnull { throw null; } + public static TValue GetValueOrDefault(this System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key) { throw null; } [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] - public static TValue GetValueOrDefault(this System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue defaultValue) where TKey : notnull { throw null; } - public static bool Remove(this System.Collections.Generic.IDictionary dictionary, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) where TKey : notnull { throw null; } - public static bool TryAdd(this System.Collections.Generic.IDictionary dictionary, TKey key, TValue value) where TKey : notnull { throw null; } + public static TValue GetValueOrDefault(this System.Collections.Generic.IReadOnlyDictionary dictionary, TKey key, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue defaultValue) { throw null; } + public static bool Remove(this System.Collections.Generic.IDictionary dictionary, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } + public static bool TryAdd(this System.Collections.Generic.IDictionary dictionary, TKey key, TValue value) { throw null; } } public abstract partial class Comparer : System.Collections.Generic.IComparer, System.Collections.IComparer { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs b/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs index 24eed76b7e3b2d..a3b0bddcb1f721 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs @@ -9,13 +9,13 @@ namespace System.Collections.Generic public static class CollectionExtensions { [return: MaybeNull] - public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key) where TKey : notnull + public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key) { return dictionary.GetValueOrDefault(key, default); } [return: MaybeNull] - public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key, [AllowNull] TValue defaultValue) where TKey : notnull + public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key, [AllowNull] TValue defaultValue) { if (dictionary == null) { @@ -26,7 +26,7 @@ public static TValue GetValueOrDefault(this IReadOnlyDictionary(this IDictionary dictionary, TKey key, TValue value) where TKey : notnull + public static bool TryAdd(this IDictionary dictionary, TKey key, TValue value) { if (dictionary == null) { @@ -42,7 +42,7 @@ public static bool TryAdd(this IDictionary dictionar return false; } - public static bool Remove(this IDictionary dictionary, TKey key, [MaybeNullWhen(false)] out TValue value) where TKey : notnull + public static bool Remove(this IDictionary dictionary, TKey key, [MaybeNullWhen(false)] out TValue value) { if (dictionary == null) {