From 137e445d4d028cd7a640ee7ad50e1f927e51689f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20Da=CC=81vid?= Date: Sat, 14 Jul 2018 12:34:21 +0200 Subject: [PATCH 1/5] Extensions for immutable builders (#21055) Added extensions for immutable builders, which should be prefered over the extensions on IEnumerable because of performance benefits. --- .../src/System/Collections/Immutable/ImmutableArray.cs | 8 ++++++++ .../System/Collections/Immutable/ImmutableDictionary.cs | 8 ++++++++ .../src/System/Collections/Immutable/ImmutableHashSet.cs | 9 +++++++++ .../src/System/Collections/Immutable/ImmutableList.cs | 8 ++++++++ .../System/Collections/Immutable/ImmutableSortedSet.cs | 8 ++++++++ 5 files changed, 41 insertions(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs index aadeceeb7eff..42bfcff4115e 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs @@ -395,6 +395,14 @@ public static ImmutableArray ToImmutableArray(this IEnumerable return CreateRange(items); } + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable array from. + /// An immutable array. + [Pure] + public static ImmutableArray ToImmutableArray(this ImmutableArray.Builder builder) => builder.ToImmutable(); + /// /// Searches an entire one-dimensional sorted for a specific element, /// using the generic interface implemented by each element diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs index 4ea02da39f59..7df3b5f2f2d7 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs @@ -166,6 +166,14 @@ public static ImmutableDictionary ToImmutableDictionary new KeyValuePair(keySelector(element), elementSelector(element)))); } + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable dictionary from. + /// An immutable dictionary. + [Pure] + public static ImmutableDictionary ToImmutableDictionary(this ImmutableDictionary.Builder builder) => builder.ToImmutable(); + /// /// Constructs an immutable dictionary based on some transformation of a sequence. /// diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs index a281afd915a6..23f166e77d0f 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs @@ -156,6 +156,15 @@ public static ImmutableHashSet ToImmutableHashSet(this IEnumer return ImmutableHashSet.Empty.WithComparer(equalityComparer).Union(source); } + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable set from. + /// An immutable set. + [Pure] + public static ImmutableHashSet ToImmutableHashSet(this ImmutableHashSet.Builder builder) => builder.ToImmutable(); + + /// /// Enumerates a sequence exactly once and produces an immutable set of its contents. /// diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs index ecb16b5f083b..68d32786c205 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs @@ -73,6 +73,14 @@ public static ImmutableList ToImmutableList(this IEnumerable.Empty.AddRange(source); } + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable list from. + /// An immutable list. + [Pure] + public static ImmutableList ToImmutableList(this ImmutableList.Builder builder) => builder.ToImmutable(); + /// /// Replaces the first equal element in the list with the specified element. /// diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs index 36a3856d77cb..424a59acf2eb 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs @@ -167,5 +167,13 @@ public static ImmutableSortedSet ToImmutableSortedSet(this IEn { return ToImmutableSortedSet(source, null); } + + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable set from. + /// An immutable set. + [Pure] + public static ImmutableSortedSet ToImmutableSortedSet(this ImmutableSortedSet.Builder builder) => builder.ToImmutable(); } } From fd9f13e6426bcce85ee45d63a8cd7331489d7148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20Da=CC=81vid?= Date: Sat, 14 Jul 2018 13:18:47 +0200 Subject: [PATCH 2/5] Extension for immutable sorted dictionary builder (#21055) --- .../Collections/Immutable/ImmutableSortedDictionary.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs index d2138fa8bfd3..5bf66d89729a 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs @@ -162,6 +162,14 @@ public static ImmutableSortedDictionary ToImmutableSortedDictionar .AddRange(source.Select(element => new KeyValuePair(keySelector(element), elementSelector(element)))); } + /// + /// Returns an immutable copy of the current contents of the builder's collection. + /// + /// The builder to create the immutable map from. + /// An immutable map. + [Pure] + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this ImmutableSortedDictionary.Builder builder) => builder.ToImmutable(); + /// /// Constructs an immutable sorted dictionary based on some transformation of a sequence. /// From ee9e1d34434402f6c54f8b3e8ed1d944b4450ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20Da=CC=81vid?= Date: Sat, 14 Jul 2018 13:19:56 +0200 Subject: [PATCH 3/5] Extensions for immutable builders in the reference api (#21055) --- .../ref/System.Collections.Immutable.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/System.Collections.Immutable/ref/System.Collections.Immutable.cs b/src/System.Collections.Immutable/ref/System.Collections.Immutable.cs index d78033ec633e..960b0fed0881 100644 --- a/src/System.Collections.Immutable/ref/System.Collections.Immutable.cs +++ b/src/System.Collections.Immutable/ref/System.Collections.Immutable.cs @@ -92,6 +92,7 @@ public static partial class ImmutableArray public static System.Collections.Immutable.ImmutableArray Create(params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableArray Create(T[] items, int start, int length) { throw null; } public static System.Collections.Immutable.ImmutableArray ToImmutableArray(this System.Collections.Generic.IEnumerable items) { throw null; } + public static System.Collections.Immutable.ImmutableArray ToImmutableArray(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } } public partial struct ImmutableArray : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.IEquatable> { @@ -270,6 +271,7 @@ public static partial class ImmutableDictionary public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) { throw null; } public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Immutable.ImmutableDictionary.Builder builder) { throw null; } } public sealed partial class ImmutableDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary { @@ -398,6 +400,7 @@ public static partial class ImmutableHashSet public static System.Collections.Immutable.ImmutableHashSet Create(params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Generic.IEnumerable source) { throw null; } public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Immutable.ImmutableHashSet.Builder builder) { throw null; } } public sealed partial class ImmutableHashSet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableSet { @@ -522,6 +525,7 @@ public static partial class ImmutableList public static System.Collections.Immutable.IImmutableList Remove(this System.Collections.Immutable.IImmutableList list, T value) { throw null; } public static System.Collections.Immutable.IImmutableList Replace(this System.Collections.Immutable.IImmutableList list, T oldValue, T newValue) { throw null; } public static System.Collections.Immutable.ImmutableList ToImmutableList(this System.Collections.Generic.IEnumerable source) { throw null; } + public static System.Collections.Immutable.ImmutableList ToImmutableList(this System.Collections.Immutable.ImmutableList.Builder builder) { throw null; } } public sealed partial class ImmutableList : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableList { @@ -746,6 +750,7 @@ public static partial class ImmutableSortedDictionary public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) { throw null; } public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer keyComparer) { throw null; } public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Immutable.ImmutableSortedDictionary.Builder source) { throw null; } } public sealed partial class ImmutableSortedDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary { @@ -881,6 +886,7 @@ public static partial class ImmutableSortedSet public static System.Collections.Immutable.ImmutableSortedSet Create(params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Generic.IEnumerable source) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IComparer comparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Immutable.ImmutableSortedSet.Builder builder) { throw null; } } public sealed partial class ImmutableSortedSet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableSet { From bbdfc66d5e7aa707295f0b64046f13b4d68ca88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20Da=CC=81vid?= Date: Sat, 14 Jul 2018 14:12:41 +0200 Subject: [PATCH 4/5] Tests for immutable builder extensions (#21055) --- .../tests/ImmutableArrayBuilderTest.cs | 20 +++++++++++++++++ .../tests/ImmutableDictionaryBuilderTest.cs | 22 +++++++++++++++++++ .../tests/ImmutableHashSetBuilderTest.cs | 22 +++++++++++++++++++ .../tests/ImmutableListBuilderTest.cs | 22 +++++++++++++++++++ .../ImmutableSortedDictionaryBuilderTest.cs | 22 +++++++++++++++++++ .../tests/ImmutableSortedSetBuilderTest.cs | 22 +++++++++++++++++++ 6 files changed, 130 insertions(+) diff --git a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs index 490a996f8618..73d32d82fa22 100644 --- a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs @@ -455,6 +455,26 @@ public void ToImmutable() Assert.True(builder.ToImmutable().IsEmpty); } + [Fact] + public void ToImmutableArray() + { + var builder = new ImmutableArray.Builder(); + builder.AddRange(0, 1, 2); + + var array = builder.ToImmutableArray(); + Assert.Equal(0, array[0]); + Assert.Equal(1, array[1]); + Assert.Equal(2, array[2]); + + builder[1] = 5; + Assert.Equal(5, builder[1]); + Assert.Equal(1, array[1]); + + builder.Clear(); + Assert.True(builder.ToImmutableArray().IsEmpty); + Assert.False(array.IsEmpty); + } + [Fact] public void CopyTo() { diff --git a/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs index e3727b3fdd68..e26427d5ac2d 100644 --- a/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs @@ -270,6 +270,28 @@ public static void TestDebuggerAttributes_Null() Assert.IsType(tie.InnerException); } + [Fact] + public void ToImmutableDictionary() + { + ImmutableDictionary.Builder builder = ImmutableDictionary.CreateBuilder(); + builder.Add(0, 0); + builder.Add(1, 1); + builder.Add(2, 2); + + var dictionary = builder.ToImmutableDictionary(); + Assert.Equal(0, dictionary[0]); + Assert.Equal(1, dictionary[1]); + Assert.Equal(2, dictionary[2]); + + builder[1] = 5; + Assert.Equal(5, builder[1]); + Assert.Equal(1, dictionary[1]); + + builder.Clear(); + Assert.True(builder.ToImmutableDictionary().IsEmpty); + Assert.False(dictionary.IsEmpty); + } + protected override IImmutableDictionary GetEmptyImmutableDictionary() { return ImmutableDictionary.Create(); diff --git a/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs index ac86a031beaf..05cef23a7a13 100644 --- a/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs @@ -308,5 +308,27 @@ public void DebuggerAttributesValid() { DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableHashSet.CreateBuilder()); } + + [Fact] + public void ToImmutableHashSet() + { + ImmutableHashSet.Builder builder = ImmutableHashSet.CreateBuilder(); + builder.Add(1); + builder.Add(2); + builder.Add(3); + + var set = builder.ToImmutableSortedSet(); + Assert.True(builder.Contains(1)); + Assert.True(builder.Contains(2)); + Assert.True(builder.Contains(3)); + + builder.Remove(3); + Assert.False(builder.Contains(3)); + Assert.True(set.Contains(3)); + + builder.Clear(); + Assert.True(builder.ToImmutableHashSet().IsEmpty); + Assert.False(set.IsEmpty); + } } } diff --git a/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs index 831fce435b52..6c2050e57928 100644 --- a/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs @@ -390,6 +390,28 @@ public void ItemRef_OutOfBounds() Assert.Throws(() => builder.ItemRef(5)); } + [Fact] + public void ToImmutableList() + { + ImmutableList.Builder builder = ImmutableList.CreateBuilder(); + builder.Add(0); + builder.Add(1); + builder.Add(2); + + var list = builder.ToImmutableList(); + Assert.Equal(0, builder[0]); + Assert.Equal(1, builder[1]); + Assert.Equal(2, builder[2]); + + builder[1] = 5; + Assert.Equal(5, builder[1]); + Assert.Equal(1, list[1]); + + builder.Clear(); + Assert.True(builder.ToImmutableList().IsEmpty); + Assert.False(list.IsEmpty); + } + protected override IEnumerable GetEnumerableOf(params T[] contents) { return ImmutableList.Empty.AddRange(contents).ToBuilder(); diff --git a/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs index 6dc3fb3d3290..7fc0849fb144 100644 --- a/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs @@ -310,6 +310,28 @@ public void ValueRef_NonExistentKey() Assert.Throws(() => builder.ValueRef("c")); } + [Fact] + public void ToImmutableSortedDictionary() + { + ImmutableSortedDictionary.Builder builder = ImmutableSortedDictionary.CreateBuilder(); + builder.Add(1, 1); + builder.Add(2, 2); + builder.Add(3, 3); + + var dictionary = builder.ToImmutableSortedDictionary(); + Assert.Equal(1, dictionary[1]); + Assert.Equal(2, dictionary[2]); + Assert.Equal(3, dictionary[3]); + + builder[2] = 5; + Assert.Equal(5, builder[2]); + Assert.Equal(2, dictionary[2]); + + builder.Clear(); + Assert.True(builder.ToImmutableSortedDictionary().IsEmpty); + Assert.False(dictionary.IsEmpty); + } + protected override IImmutableDictionary GetEmptyImmutableDictionary() { return ImmutableSortedDictionary.Create(); diff --git a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs index dd780623636b..5628bb6829ee 100644 --- a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs @@ -401,5 +401,27 @@ public void ItemRef_OutOfBounds() Assert.Throws (() => builder.ItemRef(5)); } + + [Fact] + public void ToImmutableSortedSet() + { + ImmutableSortedSet.Builder builder = ImmutableSortedSet.CreateBuilder(); + builder.Add(1); + builder.Add(5); + builder.Add(10); + + var set = builder.ToImmutableSortedSet(); + Assert.Equal(1, builder[0]); + Assert.Equal(5, builder[1]); + Assert.Equal(10, builder[2]); + + builder.Remove(10); + Assert.False(builder.Contains(10)); + Assert.True(set.Contains(10)); + + builder.Clear(); + Assert.True(builder.ToImmutableSortedSet().IsEmpty); + Assert.False(set.IsEmpty); + } } } From 4f9efa486863212d66fade5ca2839057bf77f5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20Da=CC=81vid?= Date: Sun, 15 Jul 2018 07:18:07 +0200 Subject: [PATCH 5/5] Null check in extensions for immutable collection builders (#21055) --- .../src/System/Collections/Immutable/ImmutableArray.cs | 7 ++++++- .../System/Collections/Immutable/ImmutableDictionary.cs | 7 ++++++- .../src/System/Collections/Immutable/ImmutableHashSet.cs | 7 ++++++- .../src/System/Collections/Immutable/ImmutableList.cs | 7 ++++++- .../Collections/Immutable/ImmutableSortedDictionary.cs | 7 ++++++- .../src/System/Collections/Immutable/ImmutableSortedSet.cs | 7 ++++++- .../tests/ImmutableArrayBuilderTest.cs | 3 +++ .../tests/ImmutableDictionaryBuilderTest.cs | 3 +++ .../tests/ImmutableHashSetBuilderTest.cs | 3 +++ .../tests/ImmutableListBuilderTest.cs | 3 +++ .../tests/ImmutableSortedDictionaryBuilderTest.cs | 3 +++ .../tests/ImmutableSortedSetBuilderTest.cs | 3 +++ 12 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs index 42bfcff4115e..74246fbab7ee 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs @@ -401,7 +401,12 @@ public static ImmutableArray ToImmutableArray(this IEnumerable /// The builder to create the immutable array from. /// An immutable array. [Pure] - public static ImmutableArray ToImmutableArray(this ImmutableArray.Builder builder) => builder.ToImmutable(); + public static ImmutableArray ToImmutableArray(this ImmutableArray.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } /// /// Searches an entire one-dimensional sorted for a specific element, diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs index 7df3b5f2f2d7..8b20d5cf222b 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs @@ -172,7 +172,12 @@ public static ImmutableDictionary ToImmutableDictionaryThe builder to create the immutable dictionary from. /// An immutable dictionary. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this ImmutableDictionary.Builder builder) => builder.ToImmutable(); + public static ImmutableDictionary ToImmutableDictionary(this ImmutableDictionary.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } /// /// Constructs an immutable dictionary based on some transformation of a sequence. diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs index 23f166e77d0f..b8ab82d177f2 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs @@ -162,7 +162,12 @@ public static ImmutableHashSet ToImmutableHashSet(this IEnumer /// The builder to create the immutable set from. /// An immutable set. [Pure] - public static ImmutableHashSet ToImmutableHashSet(this ImmutableHashSet.Builder builder) => builder.ToImmutable(); + public static ImmutableHashSet ToImmutableHashSet(this ImmutableHashSet.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } /// diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs index 68d32786c205..32dfa716e7c0 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs @@ -79,7 +79,12 @@ public static ImmutableList ToImmutableList(this IEnumerableThe builder to create the immutable list from. /// An immutable list. [Pure] - public static ImmutableList ToImmutableList(this ImmutableList.Builder builder) => builder.ToImmutable(); + public static ImmutableList ToImmutableList(this ImmutableList.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } /// /// Replaces the first equal element in the list with the specified element. diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs index 5bf66d89729a..fa35b6aafe5d 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs @@ -168,7 +168,12 @@ public static ImmutableSortedDictionary ToImmutableSortedDictionar /// The builder to create the immutable map from. /// An immutable map. [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this ImmutableSortedDictionary.Builder builder) => builder.ToImmutable(); + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this ImmutableSortedDictionary.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } /// /// Constructs an immutable sorted dictionary based on some transformation of a sequence. diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs index 424a59acf2eb..000e1eed2548 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs @@ -174,6 +174,11 @@ public static ImmutableSortedSet ToImmutableSortedSet(this IEn /// The builder to create the immutable set from. /// An immutable set. [Pure] - public static ImmutableSortedSet ToImmutableSortedSet(this ImmutableSortedSet.Builder builder) => builder.ToImmutable(); + public static ImmutableSortedSet ToImmutableSortedSet(this ImmutableSortedSet.Builder builder) + { + Requires.NotNull(builder, nameof(builder)); + + return builder.ToImmutable(); + } } } diff --git a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs index 73d32d82fa22..7d832ecbafae 100644 --- a/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs @@ -473,6 +473,9 @@ public void ToImmutableArray() builder.Clear(); Assert.True(builder.ToImmutableArray().IsEmpty); Assert.False(array.IsEmpty); + + ImmutableArray.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableArray()); } [Fact] diff --git a/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs index e26427d5ac2d..4390e578361a 100644 --- a/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableDictionaryBuilderTest.cs @@ -290,6 +290,9 @@ public void ToImmutableDictionary() builder.Clear(); Assert.True(builder.ToImmutableDictionary().IsEmpty); Assert.False(dictionary.IsEmpty); + + ImmutableDictionary.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableDictionary()); } protected override IImmutableDictionary GetEmptyImmutableDictionary() diff --git a/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs index 05cef23a7a13..a91587989720 100644 --- a/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableHashSetBuilderTest.cs @@ -329,6 +329,9 @@ public void ToImmutableHashSet() builder.Clear(); Assert.True(builder.ToImmutableHashSet().IsEmpty); Assert.False(set.IsEmpty); + + ImmutableHashSet.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableHashSet()); } } } diff --git a/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs index 6c2050e57928..9eeb8014f9fb 100644 --- a/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs @@ -410,6 +410,9 @@ public void ToImmutableList() builder.Clear(); Assert.True(builder.ToImmutableList().IsEmpty); Assert.False(list.IsEmpty); + + ImmutableList.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableList()); } protected override IEnumerable GetEnumerableOf(params T[] contents) diff --git a/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs index 7fc0849fb144..a863dada7518 100644 --- a/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableSortedDictionaryBuilderTest.cs @@ -330,6 +330,9 @@ public void ToImmutableSortedDictionary() builder.Clear(); Assert.True(builder.ToImmutableSortedDictionary().IsEmpty); Assert.False(dictionary.IsEmpty); + + ImmutableSortedDictionary.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableSortedDictionary()); } protected override IImmutableDictionary GetEmptyImmutableDictionary() diff --git a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs index 5628bb6829ee..8bb28d49552f 100644 --- a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs @@ -422,6 +422,9 @@ public void ToImmutableSortedSet() builder.Clear(); Assert.True(builder.ToImmutableSortedSet().IsEmpty); Assert.False(set.IsEmpty); + + ImmutableSortedSet.Builder nullBuilder = null; + AssertExtensions.Throws("builder", () => nullBuilder.ToImmutableSortedSet()); } } }