From 30e129463d7e85089f886a4221eafb1c00da12eb Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 11 Feb 2019 16:28:51 -0500 Subject: [PATCH] Switch LINQ over to using ThrowHelper pattern (dotnet/corefx#35213) Commit migrated from https://github.com/dotnet/corefx/commit/8750960d3fafa46a9b838c351e995a01fa8b599f --- .../System.Linq/src/System.Linq.csproj | 2 +- .../System.Linq/src/System/Linq/Aggregate.cs | 16 ++-- .../System.Linq/src/System/Linq/AnyAll.cs | 10 +- .../src/System/Linq/AppendPrepend.cs | 4 +- .../System.Linq/src/System/Linq/Average.cs | 80 ++++++++-------- .../System.Linq/src/System/Linq/Cast.cs | 4 +- .../System.Linq/src/System/Linq/Concat.cs | 4 +- .../System.Linq/src/System/Linq/Contains.cs | 2 +- .../System.Linq/src/System/Linq/Count.cs | 12 +-- .../System.Linq/src/System/Linq/DebugView.cs | 14 ++- .../src/System/Linq/DefaultIfEmpty.cs | 2 +- .../System.Linq/src/System/Linq/Distinct.cs | 2 +- .../System.Linq/src/System/Linq/ElementAt.cs | 7 +- .../System.Linq/src/System/Linq/Errors.cs | 23 ----- .../System.Linq/src/System/Linq/Except.cs | 8 +- .../System.Linq/src/System/Linq/First.cs | 10 +- .../System.Linq/src/System/Linq/GroupJoin.cs | 20 ++-- .../System.Linq/src/System/Linq/Grouping.cs | 94 +++++++++++++++---- .../System.Linq/src/System/Linq/Intersect.cs | 8 +- .../System.Linq/src/System/Linq/Iterator.cs | 2 +- .../System.Linq/src/System/Linq/Join.cs | 20 ++-- .../System.Linq/src/System/Linq/Last.cs | 10 +- .../System.Linq/src/System/Linq/Lookup.cs | 10 +- .../System.Linq/src/System/Linq/Max.cs | 90 +++++++++--------- .../System.Linq/src/System/Linq/Min.cs | 90 +++++++++--------- .../System.Linq/src/System/Linq/OrderBy.cs | 8 +- .../src/System/Linq/OrderedEnumerable.cs | 13 ++- .../System.Linq/src/System/Linq/Range.cs | 2 +- .../System.Linq/src/System/Linq/Repeat.cs | 2 +- .../System.Linq/src/System/Linq/Reverse.cs | 2 +- .../System.Linq/src/System/Linq/Select.cs | 8 +- .../System.Linq/src/System/Linq/SelectMany.cs | 20 ++-- .../src/System/Linq/SequenceEqual.cs | 4 +- .../System.Linq/src/System/Linq/Single.cs | 30 +++--- .../System.Linq/src/System/Linq/Skip.cs | 12 +-- .../System.Linq/src/System/Linq/Sum.cs | 60 ++++++------ .../System.Linq/src/System/Linq/Take.cs | 12 +-- .../src/System/Linq/ThrowHelper.cs | 74 +++++++++++++++ .../src/System/Linq/ToCollection.cs | 16 ++-- .../System.Linq/src/System/Linq/Union.cs | 4 +- .../System.Linq/src/System/Linq/Where.cs | 8 +- .../System.Linq/src/System/Linq/Zip.cs | 6 +- 42 files changed, 478 insertions(+), 347 deletions(-) delete mode 100644 src/libraries/System.Linq/src/System/Linq/Errors.cs create mode 100644 src/libraries/System.Linq/src/System/Linq/ThrowHelper.cs diff --git a/src/libraries/System.Linq/src/System.Linq.csproj b/src/libraries/System.Linq/src/System.Linq.csproj index 49b0cb7eae0..f41e9617671 100644 --- a/src/libraries/System.Linq/src/System.Linq.csproj +++ b/src/libraries/System.Linq/src/System.Linq.csproj @@ -71,7 +71,6 @@ - @@ -99,6 +98,7 @@ + diff --git a/src/libraries/System.Linq/src/System/Linq/Aggregate.cs b/src/libraries/System.Linq/src/System/Linq/Aggregate.cs index 121ad6d7227..c0461f0ef99 100644 --- a/src/libraries/System.Linq/src/System/Linq/Aggregate.cs +++ b/src/libraries/System.Linq/src/System/Linq/Aggregate.cs @@ -12,19 +12,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (func == null) { - throw Error.ArgumentNull(nameof(func)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } TSource result = e.Current; @@ -41,12 +41,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (func == null) { - throw Error.ArgumentNull(nameof(func)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); } TAccumulate result = seed; @@ -62,17 +62,17 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (func == null) { - throw Error.ArgumentNull(nameof(func)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } TAccumulate result = seed; diff --git a/src/libraries/System.Linq/src/System/Linq/AnyAll.cs b/src/libraries/System.Linq/src/System/Linq/AnyAll.cs index a99ecf6a618..3bbb730b623 100644 --- a/src/libraries/System.Linq/src/System/Linq/AnyAll.cs +++ b/src/libraries/System.Linq/src/System/Linq/AnyAll.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -25,12 +25,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } foreach (TSource element in source) @@ -48,12 +48,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } foreach (TSource element in source) diff --git a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs index bc7282cde2e..a9bf0a659be 100644 --- a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs +++ b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source is AppendPrependIterator appendable @@ -25,7 +25,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source is AppendPrependIterator appendable diff --git a/src/libraries/System.Linq/src/System/Linq/Average.cs b/src/libraries/System.Linq/src/System/Linq/Average.cs index a9ef6d31f88..5d26c5ec4e0 100644 --- a/src/libraries/System.Linq/src/System/Linq/Average.cs +++ b/src/libraries/System.Linq/src/System/Linq/Average.cs @@ -12,14 +12,14 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } long sum = e.Current; @@ -41,7 +41,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -78,14 +78,14 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } long sum = e.Current; @@ -107,7 +107,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -144,14 +144,14 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } double sum = e.Current; @@ -170,7 +170,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -207,14 +207,14 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } double sum = e.Current; @@ -236,7 +236,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -273,14 +273,14 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } decimal sum = e.Current; @@ -299,7 +299,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } using (IEnumerator e = source.GetEnumerator()) @@ -333,19 +333,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } long sum = selector(e.Current); @@ -367,12 +367,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) @@ -409,19 +409,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } long sum = selector(e.Current); @@ -443,12 +443,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) @@ -485,19 +485,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } double sum = selector(e.Current); @@ -516,12 +516,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) @@ -558,19 +558,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } double sum = selector(e.Current); @@ -592,12 +592,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) @@ -634,19 +634,19 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } decimal sum = selector(e.Current); @@ -665,12 +665,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } using (IEnumerator e = source.GetEnumerator()) diff --git a/src/libraries/System.Linq/src/System/Linq/Cast.cs b/src/libraries/System.Linq/src/System/Linq/Cast.cs index 4190ad3f9d5..fca2f51cba3 100644 --- a/src/libraries/System.Linq/src/System/Linq/Cast.cs +++ b/src/libraries/System.Linq/src/System/Linq/Cast.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return OfTypeIterator(source); @@ -40,7 +40,7 @@ namespace System.Linq if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return CastIterator(source); diff --git a/src/libraries/System.Linq/src/System/Linq/Concat.cs b/src/libraries/System.Linq/src/System/Linq/Concat.cs index 694b7621985..6c0aaaa7e8f 100644 --- a/src/libraries/System.Linq/src/System/Linq/Concat.cs +++ b/src/libraries/System.Linq/src/System/Linq/Concat.cs @@ -13,12 +13,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return first is ConcatIterator firstConcat diff --git a/src/libraries/System.Linq/src/System/Linq/Contains.cs b/src/libraries/System.Linq/src/System/Linq/Contains.cs index eb0e1ce57cb..12df4ca9b6c 100644 --- a/src/libraries/System.Linq/src/System/Linq/Contains.cs +++ b/src/libraries/System.Linq/src/System/Linq/Contains.cs @@ -16,7 +16,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (comparer == null) diff --git a/src/libraries/System.Linq/src/System/Linq/Count.cs b/src/libraries/System.Linq/src/System/Linq/Count.cs index 01434cbe193..45521cbf760 100644 --- a/src/libraries/System.Linq/src/System/Linq/Count.cs +++ b/src/libraries/System.Linq/src/System/Linq/Count.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is ICollection collectionoft) @@ -50,12 +50,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } int count = 0; @@ -77,7 +77,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long count = 0; @@ -99,12 +99,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } long count = 0; diff --git a/src/libraries/System.Linq/src/System/Linq/DebugView.cs b/src/libraries/System.Linq/src/System/Linq/DebugView.cs index 1957cd9b9db..79dadcef19a 100644 --- a/src/libraries/System.Linq/src/System/Linq/DebugView.cs +++ b/src/libraries/System.Linq/src/System/Linq/DebugView.cs @@ -24,7 +24,12 @@ namespace System.Linq { public SystemCore_EnumerableDebugView(IEnumerable enumerable) { - _enumerable = enumerable ?? throw Error.ArgumentNull(nameof(enumerable)); + if (enumerable is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.enumerable); + } + + _enumerable = enumerable; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] @@ -55,7 +60,12 @@ namespace System.Linq { public SystemCore_EnumerableDebugView(IEnumerable enumerable) { - _enumerable = enumerable ?? throw Error.ArgumentNull(nameof(enumerable)); + if (enumerable is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.enumerable); + } + + _enumerable = enumerable; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] diff --git a/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs b/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs index 8011d54d105..1c380755cbc 100644 --- a/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs +++ b/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs @@ -16,7 +16,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return new DefaultIfEmptyIterator(source, defaultValue); diff --git a/src/libraries/System.Linq/src/System/Linq/Distinct.cs b/src/libraries/System.Linq/src/System/Linq/Distinct.cs index 72b39525708..0cca724cb2b 100644 --- a/src/libraries/System.Linq/src/System/Linq/Distinct.cs +++ b/src/libraries/System.Linq/src/System/Linq/Distinct.cs @@ -15,7 +15,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return new DistinctIterator(source, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/ElementAt.cs b/src/libraries/System.Linq/src/System/Linq/ElementAt.cs index 75f37dfada0..9c47ee8c6fa 100644 --- a/src/libraries/System.Linq/src/System/Linq/ElementAt.cs +++ b/src/libraries/System.Linq/src/System/Linq/ElementAt.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IPartition partition) @@ -47,14 +47,15 @@ namespace System.Linq } } - throw Error.ArgumentOutOfRange(nameof(index)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); + return default; } public static TSource ElementAtOrDefault(this IEnumerable source, int index) { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IPartition partition) diff --git a/src/libraries/System.Linq/src/System/Linq/Errors.cs b/src/libraries/System.Linq/src/System/Linq/Errors.cs deleted file mode 100644 index 8c2c2a90646..00000000000 --- a/src/libraries/System.Linq/src/System/Linq/Errors.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System.Linq -{ - internal static class Error - { - internal static Exception ArgumentNull(string s) => new ArgumentNullException(s); - - internal static Exception ArgumentOutOfRange(string s) => new ArgumentOutOfRangeException(s); - - internal static Exception MoreThanOneElement() => new InvalidOperationException(SR.MoreThanOneElement); - - internal static Exception MoreThanOneMatch() => new InvalidOperationException(SR.MoreThanOneMatch); - - internal static Exception NoElements() => new InvalidOperationException(SR.NoElements); - - internal static Exception NoMatch() => new InvalidOperationException(SR.NoMatch); - - internal static Exception NotSupported() => new NotSupportedException(); - } -} diff --git a/src/libraries/System.Linq/src/System/Linq/Except.cs b/src/libraries/System.Linq/src/System/Linq/Except.cs index f1b4e1c8482..e30b074709a 100644 --- a/src/libraries/System.Linq/src/System/Linq/Except.cs +++ b/src/libraries/System.Linq/src/System/Linq/Except.cs @@ -12,12 +12,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return ExceptIterator(first, second, null); @@ -27,12 +27,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return ExceptIterator(first, second, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/First.cs b/src/libraries/System.Linq/src/System/Linq/First.cs index 190ba96a8a2..41e7b1379d6 100644 --- a/src/libraries/System.Linq/src/System/Linq/First.cs +++ b/src/libraries/System.Linq/src/System/Linq/First.cs @@ -13,7 +13,7 @@ namespace System.Linq TSource first = source.TryGetFirst(out bool found); if (!found) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } return first; @@ -24,7 +24,7 @@ namespace System.Linq TSource first = source.TryGetFirst(predicate, out bool found); if (!found) { - throw Error.NoMatch(); + ThrowHelper.ThrowNoMatchException(); } return first; @@ -40,7 +40,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IPartition partition) @@ -76,12 +76,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } if (source is OrderedEnumerable ordered) diff --git a/src/libraries/System.Linq/src/System/Linq/GroupJoin.cs b/src/libraries/System.Linq/src/System/Linq/GroupJoin.cs index cc9afd5fecb..f5eb35045cb 100644 --- a/src/libraries/System.Linq/src/System/Linq/GroupJoin.cs +++ b/src/libraries/System.Linq/src/System/Linq/GroupJoin.cs @@ -12,27 +12,27 @@ namespace System.Linq { if (outer == null) { - throw Error.ArgumentNull(nameof(outer)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outer); } if (inner == null) { - throw Error.ArgumentNull(nameof(inner)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.inner); } if (outerKeySelector == null) { - throw Error.ArgumentNull(nameof(outerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outerKeySelector); } if (innerKeySelector == null) { - throw Error.ArgumentNull(nameof(innerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.innerKeySelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return GroupJoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, null); @@ -42,27 +42,27 @@ namespace System.Linq { if (outer == null) { - throw Error.ArgumentNull(nameof(outer)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outer); } if (inner == null) { - throw Error.ArgumentNull(nameof(inner)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.inner); } if (outerKeySelector == null) { - throw Error.ArgumentNull(nameof(outerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outerKeySelector); } if (innerKeySelector == null) { - throw Error.ArgumentNull(nameof(innerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.innerKeySelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return GroupJoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/Grouping.cs b/src/libraries/System.Linq/src/System/Linq/Grouping.cs index 76660e4caf2..1c435816376 100644 --- a/src/libraries/System.Linq/src/System/Linq/Grouping.cs +++ b/src/libraries/System.Linq/src/System/Linq/Grouping.cs @@ -98,22 +98,26 @@ namespace System.Linq bool ICollection.IsReadOnly => true; - void ICollection.Add(TElement item) => throw Error.NotSupported(); + void ICollection.Add(TElement item) => ThrowHelper.ThrowNotSupportedException(); - void ICollection.Clear() => throw Error.NotSupported(); + void ICollection.Clear() => ThrowHelper.ThrowNotSupportedException(); bool ICollection.Contains(TElement item) => Array.IndexOf(_elements, item, 0, _count) >= 0; void ICollection.CopyTo(TElement[] array, int arrayIndex) => Array.Copy(_elements, 0, array, arrayIndex, _count); - bool ICollection.Remove(TElement item) => throw Error.NotSupported(); + bool ICollection.Remove(TElement item) + { + ThrowHelper.ThrowNotSupportedException(); + return false; + } int IList.IndexOf(TElement item) => Array.IndexOf(_elements, item, 0, _count); - void IList.Insert(int index, TElement item) => throw Error.NotSupported(); + void IList.Insert(int index, TElement item) => ThrowHelper.ThrowNotSupportedException(); - void IList.RemoveAt(int index) => throw Error.NotSupported(); + void IList.RemoveAt(int index) => ThrowHelper.ThrowNotSupportedException(); TElement IList.this[int index] { @@ -121,7 +125,7 @@ namespace System.Linq { if (index < 0 || index >= _count) { - throw Error.ArgumentOutOfRange(nameof(index)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } return _elements[index]; @@ -129,7 +133,7 @@ namespace System.Linq set { - throw Error.NotSupported(); + ThrowHelper.ThrowNotSupportedException(); } } } @@ -144,11 +148,28 @@ namespace System.Linq public GroupedResultEnumerable(IEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer) { - _source = source ?? throw Error.ArgumentNull(nameof(source)); - _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector)); - _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector)); + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + if (keySelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); + } + if (elementSelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector); + } + if (resultSelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); + } + + _source = source; + _keySelector = keySelector; + _elementSelector = elementSelector; _comparer = comparer; - _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector)); + _resultSelector = resultSelector; } public IEnumerator GetEnumerator() @@ -169,9 +190,22 @@ namespace System.Linq public GroupedResultEnumerable(IEnumerable source, Func keySelector, Func, TResult> resultSelector, IEqualityComparer comparer) { - _source = source ?? throw Error.ArgumentNull(nameof(source)); - _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector)); - _resultSelector = resultSelector ?? throw Error.ArgumentNull(nameof(resultSelector)); + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + if (keySelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); + } + if (resultSelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); + } + + _source = source; + _keySelector = keySelector; + _resultSelector = resultSelector; _comparer = comparer; } @@ -193,9 +227,22 @@ namespace System.Linq public GroupedEnumerable(IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) { - _source = source ?? throw Error.ArgumentNull(nameof(source)); - _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector)); - _elementSelector = elementSelector ?? throw Error.ArgumentNull(nameof(elementSelector)); + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + if (keySelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); + } + if (elementSelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector); + } + + _source = source; + _keySelector = keySelector; + _elementSelector = elementSelector; _comparer = comparer; } @@ -213,8 +260,17 @@ namespace System.Linq public GroupedEnumerable(IEnumerable source, Func keySelector, IEqualityComparer comparer) { - _source = source ?? throw Error.ArgumentNull(nameof(source)); - _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector)); + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + if (keySelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); + } + + _source = source; + _keySelector = keySelector; _comparer = comparer; } diff --git a/src/libraries/System.Linq/src/System/Linq/Intersect.cs b/src/libraries/System.Linq/src/System/Linq/Intersect.cs index 2e2cd2d8edb..d83b9c3537f 100644 --- a/src/libraries/System.Linq/src/System/Linq/Intersect.cs +++ b/src/libraries/System.Linq/src/System/Linq/Intersect.cs @@ -12,12 +12,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return IntersectIterator(first, second, null); @@ -27,12 +27,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return IntersectIterator(first, second, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/Iterator.cs b/src/libraries/System.Linq/src/System/Linq/Iterator.cs index 76466e66281..c99066ce519 100644 --- a/src/libraries/System.Linq/src/System/Linq/Iterator.cs +++ b/src/libraries/System.Linq/src/System/Linq/Iterator.cs @@ -113,7 +113,7 @@ namespace System.Linq IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - void IEnumerator.Reset() => throw Error.NotSupported(); + void IEnumerator.Reset() => ThrowHelper.ThrowNotSupportedException(); } } } diff --git a/src/libraries/System.Linq/src/System/Linq/Join.cs b/src/libraries/System.Linq/src/System/Linq/Join.cs index e9e900fec3d..3f0a7d900aa 100644 --- a/src/libraries/System.Linq/src/System/Linq/Join.cs +++ b/src/libraries/System.Linq/src/System/Linq/Join.cs @@ -12,27 +12,27 @@ namespace System.Linq { if (outer == null) { - throw Error.ArgumentNull(nameof(outer)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outer); } if (inner == null) { - throw Error.ArgumentNull(nameof(inner)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.inner); } if (outerKeySelector == null) { - throw Error.ArgumentNull(nameof(outerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outerKeySelector); } if (innerKeySelector == null) { - throw Error.ArgumentNull(nameof(innerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.innerKeySelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return JoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, null); @@ -42,27 +42,27 @@ namespace System.Linq { if (outer == null) { - throw Error.ArgumentNull(nameof(outer)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outer); } if (inner == null) { - throw Error.ArgumentNull(nameof(inner)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.inner); } if (outerKeySelector == null) { - throw Error.ArgumentNull(nameof(outerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.outerKeySelector); } if (innerKeySelector == null) { - throw Error.ArgumentNull(nameof(innerKeySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.innerKeySelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return JoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/Last.cs b/src/libraries/System.Linq/src/System/Linq/Last.cs index c6be8e13b2d..bb25495f237 100644 --- a/src/libraries/System.Linq/src/System/Linq/Last.cs +++ b/src/libraries/System.Linq/src/System/Linq/Last.cs @@ -13,7 +13,7 @@ namespace System.Linq TSource last = source.TryGetLast(out bool found); if (!found) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } return last; @@ -24,7 +24,7 @@ namespace System.Linq TSource last = source.TryGetLast(predicate, out bool found); if (!found) { - throw Error.NoMatch(); + ThrowHelper.ThrowNoMatchException(); } return last; @@ -40,7 +40,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IPartition partition) @@ -84,12 +84,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } if (source is OrderedEnumerable ordered) diff --git a/src/libraries/System.Linq/src/System/Linq/Lookup.cs b/src/libraries/System.Linq/src/System/Linq/Lookup.cs index c4f5d15b79a..b0169e5286f 100644 --- a/src/libraries/System.Linq/src/System/Linq/Lookup.cs +++ b/src/libraries/System.Linq/src/System/Linq/Lookup.cs @@ -17,12 +17,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (keySelector == null) { - throw Error.ArgumentNull(nameof(keySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); } return Lookup.Create(source, keySelector, comparer); @@ -35,17 +35,17 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (keySelector == null) { - throw Error.ArgumentNull(nameof(keySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); } if (elementSelector == null) { - throw Error.ArgumentNull(nameof(elementSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector); } return Lookup.Create(source, keySelector, elementSelector, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/Max.cs b/src/libraries/System.Linq/src/System/Linq/Max.cs index 5a15ae02513..c2fc7f57604 100644 --- a/src/libraries/System.Linq/src/System/Linq/Max.cs +++ b/src/libraries/System.Linq/src/System/Linq/Max.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int value; @@ -20,7 +20,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -41,7 +41,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int? value = null; @@ -103,7 +103,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long value; @@ -111,7 +111,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -132,7 +132,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long? value = null; @@ -188,7 +188,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double value; @@ -196,7 +196,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -232,7 +232,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double? value = null; @@ -286,7 +286,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } float value; @@ -294,7 +294,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -325,7 +325,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } float? value = null; @@ -379,7 +379,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal value; @@ -387,7 +387,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -408,7 +408,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal? value = null; @@ -445,7 +445,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } Comparer comparer = Comparer.Default; @@ -481,7 +481,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -503,12 +503,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int value; @@ -516,7 +516,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -537,12 +537,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int? value = null; @@ -604,12 +604,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } long value; @@ -617,7 +617,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -638,12 +638,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } long? value = null; @@ -699,12 +699,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } float value; @@ -712,7 +712,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -743,12 +743,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } float? value = null; @@ -802,12 +802,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double value; @@ -815,7 +815,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -851,12 +851,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double? value = null; @@ -910,12 +910,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal value; @@ -923,7 +923,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -944,12 +944,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal? value = null; @@ -986,12 +986,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } Comparer comparer = Comparer.Default; @@ -1027,7 +1027,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); diff --git a/src/libraries/System.Linq/src/System/Linq/Min.cs b/src/libraries/System.Linq/src/System/Linq/Min.cs index 4249a84f2a9..97f942fc16f 100644 --- a/src/libraries/System.Linq/src/System/Linq/Min.cs +++ b/src/libraries/System.Linq/src/System/Linq/Min.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int value; @@ -20,7 +20,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -41,7 +41,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int? value = null; @@ -85,7 +85,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long value; @@ -93,7 +93,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -114,7 +114,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long? value = null; @@ -154,7 +154,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } float value; @@ -162,7 +162,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -196,7 +196,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } float? value = null; @@ -240,7 +240,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double value; @@ -248,7 +248,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -273,7 +273,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double? value = null; @@ -317,7 +317,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal value; @@ -325,7 +325,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -346,7 +346,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal? value = null; @@ -383,7 +383,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } Comparer comparer = Comparer.Default; @@ -419,7 +419,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = e.Current; @@ -441,12 +441,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int value; @@ -454,7 +454,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -475,12 +475,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int? value = null; @@ -524,12 +524,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } long value; @@ -537,7 +537,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -558,12 +558,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } long? value = null; @@ -603,12 +603,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } float value; @@ -616,7 +616,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -650,12 +650,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } float? value = null; @@ -699,12 +699,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double value; @@ -712,7 +712,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -737,12 +737,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double? value = null; @@ -786,12 +786,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal value; @@ -799,7 +799,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); @@ -820,12 +820,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal? value = null; @@ -862,12 +862,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } Comparer comparer = Comparer.Default; @@ -903,7 +903,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } value = selector(e.Current); diff --git a/src/libraries/System.Linq/src/System/Linq/OrderBy.cs b/src/libraries/System.Linq/src/System/Linq/OrderBy.cs index b2dbbf0b4e1..9c9a77be023 100644 --- a/src/libraries/System.Linq/src/System/Linq/OrderBy.cs +++ b/src/libraries/System.Linq/src/System/Linq/OrderBy.cs @@ -24,7 +24,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source.CreateOrderedEnumerable(keySelector, null, false); @@ -34,7 +34,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source.CreateOrderedEnumerable(keySelector, comparer, false); @@ -44,7 +44,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source.CreateOrderedEnumerable(keySelector, null, true); @@ -54,7 +54,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source.CreateOrderedEnumerable(keySelector, comparer, true); diff --git a/src/libraries/System.Linq/src/System/Linq/OrderedEnumerable.cs b/src/libraries/System.Linq/src/System/Linq/OrderedEnumerable.cs index 0500b7110c7..54d13f82f45 100644 --- a/src/libraries/System.Linq/src/System/Linq/OrderedEnumerable.cs +++ b/src/libraries/System.Linq/src/System/Linq/OrderedEnumerable.cs @@ -145,9 +145,18 @@ namespace System.Linq internal OrderedEnumerable(IEnumerable source, Func keySelector, IComparer comparer, bool descending, OrderedEnumerable parent) { - _source = source ?? throw Error.ArgumentNull(nameof(source)); + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + if (keySelector is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); + } + + _source = source; _parent = parent; - _keySelector = keySelector ?? throw Error.ArgumentNull(nameof(keySelector)); + _keySelector = keySelector; _comparer = comparer ?? Comparer.Default; _descending = descending; } diff --git a/src/libraries/System.Linq/src/System/Linq/Range.cs b/src/libraries/System.Linq/src/System/Linq/Range.cs index 1260a1f74ed..a26ed82281e 100644 --- a/src/libraries/System.Linq/src/System/Linq/Range.cs +++ b/src/libraries/System.Linq/src/System/Linq/Range.cs @@ -14,7 +14,7 @@ namespace System.Linq long max = ((long)start) + count - 1; if (count < 0 || max > int.MaxValue) { - throw Error.ArgumentOutOfRange(nameof(count)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count); } if (count == 0) diff --git a/src/libraries/System.Linq/src/System/Linq/Repeat.cs b/src/libraries/System.Linq/src/System/Linq/Repeat.cs index 8ffa47a8775..76e2ce4864d 100644 --- a/src/libraries/System.Linq/src/System/Linq/Repeat.cs +++ b/src/libraries/System.Linq/src/System/Linq/Repeat.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (count < 0) { - throw Error.ArgumentOutOfRange(nameof(count)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count); } if (count == 0) diff --git a/src/libraries/System.Linq/src/System/Linq/Reverse.cs b/src/libraries/System.Linq/src/System/Linq/Reverse.cs index 29ae52b8180..3c120028338 100644 --- a/src/libraries/System.Linq/src/System/Linq/Reverse.cs +++ b/src/libraries/System.Linq/src/System/Linq/Reverse.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return new ReverseIterator(source); diff --git a/src/libraries/System.Linq/src/System/Linq/Select.cs b/src/libraries/System.Linq/src/System/Linq/Select.cs index 942d07563c5..2cccaf0afe5 100644 --- a/src/libraries/System.Linq/src/System/Linq/Select.cs +++ b/src/libraries/System.Linq/src/System/Linq/Select.cs @@ -15,12 +15,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } if (source is Iterator iterator) @@ -65,12 +65,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } return SelectIterator(source, selector); diff --git a/src/libraries/System.Linq/src/System/Linq/SelectMany.cs b/src/libraries/System.Linq/src/System/Linq/SelectMany.cs index 75ca9308a0c..2ef2e3d5d3b 100644 --- a/src/libraries/System.Linq/src/System/Linq/SelectMany.cs +++ b/src/libraries/System.Linq/src/System/Linq/SelectMany.cs @@ -13,12 +13,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } return new SelectManySingleSelectorIterator(source, selector); @@ -28,12 +28,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } return SelectManyIterator(source, selector); @@ -60,17 +60,17 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (collectionSelector == null) { - throw Error.ArgumentNull(nameof(collectionSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collectionSelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return SelectManyIterator(source, collectionSelector, resultSelector); @@ -97,17 +97,17 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (collectionSelector == null) { - throw Error.ArgumentNull(nameof(collectionSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collectionSelector); } if (resultSelector == null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return SelectManyIterator(source, collectionSelector, resultSelector); diff --git a/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs b/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs index df8600f4771..262a1188b48 100644 --- a/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs +++ b/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs @@ -20,12 +20,12 @@ namespace System.Linq if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } if (first is ICollection firstCol && second is ICollection secondCol) diff --git a/src/libraries/System.Linq/src/System/Linq/Single.cs b/src/libraries/System.Linq/src/System/Linq/Single.cs index 3874f81042c..fdeb9ce2f8e 100644 --- a/src/libraries/System.Linq/src/System/Linq/Single.cs +++ b/src/libraries/System.Linq/src/System/Linq/Single.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IList list) @@ -20,7 +20,8 @@ namespace System.Linq switch (list.Count) { case 0: - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); + return default; case 1: return list[0]; } @@ -31,7 +32,7 @@ namespace System.Linq { if (!e.MoveNext()) { - throw Error.NoElements(); + ThrowHelper.ThrowNoElementsException(); } TSource result = e.Current; @@ -42,19 +43,20 @@ namespace System.Linq } } - throw Error.MoreThanOneElement(); + ThrowHelper.ThrowMoreThanOneElementException(); + return default; } public static TSource Single(this IEnumerable source, Func predicate) { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } using (IEnumerator e = source.GetEnumerator()) @@ -68,7 +70,7 @@ namespace System.Linq { if (predicate(e.Current)) { - throw Error.MoreThanOneMatch(); + ThrowHelper.ThrowMoreThanOneMatchException(); } } @@ -77,14 +79,15 @@ namespace System.Linq } } - throw Error.NoMatch(); + ThrowHelper.ThrowNoMatchException(); + return default; } public static TSource SingleOrDefault(this IEnumerable source) { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (source is IList list) @@ -114,19 +117,20 @@ namespace System.Linq } } - throw Error.MoreThanOneElement(); + ThrowHelper.ThrowMoreThanOneElementException(); + return default; } public static TSource SingleOrDefault(this IEnumerable source, Func predicate) { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } using (IEnumerator e = source.GetEnumerator()) @@ -140,7 +144,7 @@ namespace System.Linq { if (predicate(e.Current)) { - throw Error.MoreThanOneMatch(); + ThrowHelper.ThrowMoreThanOneMatchException(); } } diff --git a/src/libraries/System.Linq/src/System/Linq/Skip.cs b/src/libraries/System.Linq/src/System/Linq/Skip.cs index 99bd870255b..bf854eedd14 100644 --- a/src/libraries/System.Linq/src/System/Linq/Skip.cs +++ b/src/libraries/System.Linq/src/System/Linq/Skip.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (count <= 0) @@ -39,12 +39,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } return SkipWhileIterator(source, predicate); @@ -75,12 +75,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } return SkipWhileIterator(source, predicate); @@ -117,7 +117,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (count <= 0) diff --git a/src/libraries/System.Linq/src/System/Linq/Sum.cs b/src/libraries/System.Linq/src/System/Linq/Sum.cs index 3c29a269470..74eb73e3d28 100644 --- a/src/libraries/System.Linq/src/System/Linq/Sum.cs +++ b/src/libraries/System.Linq/src/System/Linq/Sum.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int sum = 0; @@ -31,7 +31,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } int sum = 0; @@ -53,7 +53,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long sum = 0; @@ -72,7 +72,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long sum = 0; @@ -94,7 +94,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double sum = 0; @@ -110,7 +110,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double sum = 0; @@ -129,7 +129,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double sum = 0; @@ -145,7 +145,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } double sum = 0; @@ -164,7 +164,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal sum = 0; @@ -180,7 +180,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } decimal sum = 0; @@ -199,12 +199,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int sum = 0; @@ -223,12 +223,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } int sum = 0; @@ -251,12 +251,12 @@ namespace System.Linq { if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } long sum = 0; @@ -275,12 +275,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } long sum = 0; @@ -303,12 +303,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double sum = 0; @@ -324,12 +324,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double sum = 0; @@ -349,12 +349,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double sum = 0; @@ -370,12 +370,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } double sum = 0; @@ -395,12 +395,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal sum = 0; @@ -416,12 +416,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (selector == null) { - throw Error.ArgumentNull(nameof(selector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector); } decimal sum = 0; diff --git a/src/libraries/System.Linq/src/System/Linq/Take.cs b/src/libraries/System.Linq/src/System/Linq/Take.cs index ce5d2018e8c..0f67ef67020 100644 --- a/src/libraries/System.Linq/src/System/Linq/Take.cs +++ b/src/libraries/System.Linq/src/System/Linq/Take.cs @@ -13,7 +13,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return count <= 0 ? @@ -25,12 +25,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } return TakeWhileIterator(source, predicate); @@ -53,12 +53,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } return TakeWhileIterator(source, predicate); @@ -87,7 +87,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return count <= 0 ? diff --git a/src/libraries/System.Linq/src/System/Linq/ThrowHelper.cs b/src/libraries/System.Linq/src/System/Linq/ThrowHelper.cs new file mode 100644 index 00000000000..e39381b0cf6 --- /dev/null +++ b/src/libraries/System.Linq/src/System/Linq/ThrowHelper.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Runtime.CompilerServices; + +namespace System.Linq +{ + internal static class ThrowHelper + { + internal static void ThrowArgumentNullException(ExceptionArgument argument) => throw new ArgumentNullException(GetArgumentString(argument)); + + internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) => throw new ArgumentOutOfRangeException(GetArgumentString(argument)); + + internal static void ThrowMoreThanOneElementException() => throw new InvalidOperationException(SR.MoreThanOneElement); + + internal static void ThrowMoreThanOneMatchException() => throw new InvalidOperationException(SR.MoreThanOneMatch); + + internal static void ThrowNoElementsException() => throw new InvalidOperationException(SR.NoElements); + + internal static void ThrowNoMatchException() => throw new InvalidOperationException(SR.NoMatch); + + internal static void ThrowNotSupportedException() => throw new NotSupportedException(); + + private static string GetArgumentString(ExceptionArgument argument) + { + switch (argument) + { + case ExceptionArgument.collectionSelector: return nameof(ExceptionArgument.collectionSelector); + case ExceptionArgument.count: return nameof(ExceptionArgument.count); + case ExceptionArgument.elementSelector: return nameof(ExceptionArgument.elementSelector); + case ExceptionArgument.enumerable: return nameof(ExceptionArgument.enumerable); + case ExceptionArgument.first: return nameof(ExceptionArgument.first); + case ExceptionArgument.func: return nameof(ExceptionArgument.func); + case ExceptionArgument.index: return nameof(ExceptionArgument.index); + case ExceptionArgument.inner: return nameof(ExceptionArgument.inner); + case ExceptionArgument.innerKeySelector: return nameof(ExceptionArgument.innerKeySelector); + case ExceptionArgument.keySelector: return nameof(ExceptionArgument.keySelector); + case ExceptionArgument.outer: return nameof(ExceptionArgument.outer); + case ExceptionArgument.outerKeySelector: return nameof(ExceptionArgument.outerKeySelector); + case ExceptionArgument.predicate: return nameof(ExceptionArgument.predicate); + case ExceptionArgument.resultSelector: return nameof(ExceptionArgument.resultSelector); + case ExceptionArgument.second: return nameof(ExceptionArgument.second); + case ExceptionArgument.selector: return nameof(ExceptionArgument.selector); + case ExceptionArgument.source: return nameof(ExceptionArgument.source); + default: + Debug.Fail("The ExceptionArgument value is not defined."); + return string.Empty; + } + } + } + + internal enum ExceptionArgument + { + collectionSelector, + count, + elementSelector, + enumerable, + first, + func, + index, + inner, + innerKeySelector, + keySelector, + outer, + outerKeySelector, + predicate, + resultSelector, + second, + selector, + source, + } +} diff --git a/src/libraries/System.Linq/src/System/Linq/ToCollection.cs b/src/libraries/System.Linq/src/System/Linq/ToCollection.cs index 0ed6184b1a2..d6ad5008f00 100644 --- a/src/libraries/System.Linq/src/System/Linq/ToCollection.cs +++ b/src/libraries/System.Linq/src/System/Linq/ToCollection.cs @@ -12,7 +12,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source is IIListProvider arrayProvider @@ -24,7 +24,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } return source is IIListProvider listProvider ? listProvider.ToList() : new List(source); @@ -37,12 +37,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (keySelector == null) { - throw Error.ArgumentNull(nameof(keySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); } int capacity = 0; @@ -103,17 +103,17 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (keySelector == null) { - throw Error.ArgumentNull(nameof(keySelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector); } if (elementSelector == null) { - throw Error.ArgumentNull(nameof(elementSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector); } int capacity = 0; @@ -173,7 +173,7 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } // Don't pre-allocate based on knowledge of size, as potentially many elements will be dropped. diff --git a/src/libraries/System.Linq/src/System/Linq/Union.cs b/src/libraries/System.Linq/src/System/Linq/Union.cs index a63121eee6c..f4a5d9d000d 100644 --- a/src/libraries/System.Linq/src/System/Linq/Union.cs +++ b/src/libraries/System.Linq/src/System/Linq/Union.cs @@ -16,12 +16,12 @@ namespace System.Linq { if (first == null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second == null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } return first is UnionIterator union && AreEqualityComparersEqual(comparer, union._comparer) ? union.Union(second) : new UnionIterator2(first, second, comparer); diff --git a/src/libraries/System.Linq/src/System/Linq/Where.cs b/src/libraries/System.Linq/src/System/Linq/Where.cs index fe63843d603..80c8a014c36 100644 --- a/src/libraries/System.Linq/src/System/Linq/Where.cs +++ b/src/libraries/System.Linq/src/System/Linq/Where.cs @@ -14,12 +14,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } if (source is Iterator iterator) @@ -46,12 +46,12 @@ namespace System.Linq { if (source == null) { - throw Error.ArgumentNull(nameof(source)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); } if (predicate == null) { - throw Error.ArgumentNull(nameof(predicate)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate); } return WhereIterator(source, predicate); diff --git a/src/libraries/System.Linq/src/System/Linq/Zip.cs b/src/libraries/System.Linq/src/System/Linq/Zip.cs index c49337cb5dc..19c4dd4c253 100644 --- a/src/libraries/System.Linq/src/System/Linq/Zip.cs +++ b/src/libraries/System.Linq/src/System/Linq/Zip.cs @@ -12,17 +12,17 @@ namespace System.Linq { if (first is null) { - throw Error.ArgumentNull(nameof(first)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first); } if (second is null) { - throw Error.ArgumentNull(nameof(second)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second); } if (resultSelector is null) { - throw Error.ArgumentNull(nameof(resultSelector)); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); } return ZipIterator(first, second, resultSelector); -- 2.34.1