From 80ab81c9e06af0409e621ddeef7f83e4f4ebf0b9 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 17 Nov 2023 19:12:34 +0100 Subject: [PATCH] Update to using C# 12 --- Directory.Build.props | 2 +- MoreLinq.Test/AppendTest.cs | 2 +- MoreLinq.Test/AssertCountTest.cs | 12 +--- MoreLinq.Test/AssertTest.cs | 5 +- MoreLinq.Test/Async/WatchableEnumerator.cs | 8 +-- MoreLinq.Test/BatchTest.cs | 2 +- MoreLinq.Test/BreakingCollection.cs | 6 +- MoreLinq.Test/BreakingList.cs | 7 +- MoreLinq.Test/BreakingReadOnlyCollection.cs | 7 +- MoreLinq.Test/BreakingReadOnlyList.cs | 8 +-- MoreLinq.Test/CountDownTest.cs | 40 +++++------- MoreLinq.Test/DistinctByTest.cs | 6 +- MoreLinq.Test/ExceptByTest.cs | 16 ++--- MoreLinq.Test/FlattenTest.cs | 30 +++------ MoreLinq.Test/GroupAdjacentTest.cs | 2 +- MoreLinq.Test/PermutationsTest.cs | 72 ++++++++++----------- MoreLinq.Test/PrependTest.cs | 6 +- MoreLinq.Test/ReadOnlyCollection.cs | 7 +- MoreLinq.Test/SequenceReader.cs | 18 ++---- MoreLinq.Test/SubsetTest.cs | 52 +++++++-------- MoreLinq.Test/TestException.cs | 3 +- MoreLinq.Test/ToDataTableTest.cs | 19 ++---- MoreLinq.Test/TransposeTest.cs | 54 ++++++++-------- MoreLinq.Test/TraverseTest.cs | 12 +--- MoreLinq.Test/TrySingleTest.cs | 16 ++--- MoreLinq.Test/WatchableEnumerator.cs | 8 +-- MoreLinq/Acquire.cs | 2 +- MoreLinq/Collections/Dictionary.cs | 10 ++- MoreLinq/CountBy.cs | 4 +- MoreLinq/Delegating.cs | 29 +++------ MoreLinq/EmptyArray.cs | 14 ---- MoreLinq/EquiZip.cs | 4 +- MoreLinq/Experimental/Async/Merge.cs | 6 +- MoreLinq/Experimental/Await.cs | 18 ++---- MoreLinq/Experimental/Batch.cs | 4 +- MoreLinq/Experimental/Memoize.cs | 12 +--- MoreLinq/FillBackward.cs | 2 +- MoreLinq/GroupAdjacent.cs | 15 ++--- MoreLinq/Maxima.cs | 28 +++----- MoreLinq/Partition.cs | 2 +- MoreLinq/PendNode.cs | 5 +- MoreLinq/Reactive/Subject.cs | 2 +- MoreLinq/Return.cs | 6 +- MoreLinq/ReverseComparer.cs | 7 +- MoreLinq/Segment.cs | 2 +- MoreLinq/SequenceException.cs | 2 + MoreLinq/SortedMerge.cs | 7 +- MoreLinq/Split.cs | 2 +- MoreLinq/ToArrayByIndex.cs | 4 +- MoreLinq/ToDataTable.cs | 2 +- bld/ExtensionsGenerator/Program.cs | 29 ++++----- 51 files changed, 260 insertions(+), 378 deletions(-) delete mode 100644 MoreLinq/EmptyArray.cs diff --git a/Directory.Build.props b/Directory.Build.props index 9fc11a600..222a1c0a6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 11 + 12 enable true 8.0-all diff --git a/MoreLinq.Test/AppendTest.cs b/MoreLinq.Test/AppendTest.cs index 58dbd6c9d..0398a5658 100644 --- a/MoreLinq.Test/AppendTest.cs +++ b/MoreLinq.Test/AppendTest.cs @@ -37,7 +37,7 @@ public void AppendWithNonEmptyHeadSequence() [Test] public void AppendWithEmptyHeadSequence() { - string[] head = { }; + string[] head = []; var tail = "first"; var whole = head.Append(tail); whole.AssertSequenceEqual("first"); diff --git a/MoreLinq.Test/AssertCountTest.cs b/MoreLinq.Test/AssertCountTest.cs index b8dfe839d..05e5ae182 100644 --- a/MoreLinq.Test/AssertCountTest.cs +++ b/MoreLinq.Test/AssertCountTest.cs @@ -90,16 +90,10 @@ public void AssertCountShortSequenceWithErrorSelector() .And.Count.EqualTo(4)); } - sealed class TestException : Exception + sealed class TestException(int cmp, int count) : Exception { - public int Cmp { get; } - public int Count { get; } - - public TestException(int cmp, int count) - { - Cmp = cmp; - Count = count; - } + public int Cmp { get; } = cmp; + public int Count { get; } = count; } [Test] diff --git a/MoreLinq.Test/AssertTest.cs b/MoreLinq.Test/AssertTest.cs index ecd288ce6..ad037a030 100644 --- a/MoreLinq.Test/AssertTest.cs +++ b/MoreLinq.Test/AssertTest.cs @@ -63,10 +63,9 @@ public void AssertSequenceWithInvalidElementsAndCustomError() .With.Property(nameof(ValueException.Value)).EqualTo(7)); } - sealed class ValueException : Exception + sealed class ValueException(object value) : Exception { - public object Value { get; } - public ValueException(object value) => Value = value; + public object Value { get; } = value; } } } diff --git a/MoreLinq.Test/Async/WatchableEnumerator.cs b/MoreLinq.Test/Async/WatchableEnumerator.cs index 0920b36e2..e4635533f 100644 --- a/MoreLinq.Test/Async/WatchableEnumerator.cs +++ b/MoreLinq.Test/Async/WatchableEnumerator.cs @@ -26,16 +26,14 @@ partial class TestExtensions public static WatchableEnumerator AsWatchable(this IAsyncEnumerator source) => new(source); } - sealed class WatchableEnumerator : IAsyncEnumerator + sealed class WatchableEnumerator(IAsyncEnumerator source) : + IAsyncEnumerator { - readonly IAsyncEnumerator _source; + readonly IAsyncEnumerator _source = source ?? throw new ArgumentNullException(nameof(source)); public event EventHandler? Disposed; public event EventHandler? MoveNextCalled; - public WatchableEnumerator(IAsyncEnumerator source) => - _source = source ?? throw new ArgumentNullException(nameof(source)); - public T Current => _source.Current; public async ValueTask MoveNextAsync() diff --git a/MoreLinq.Test/BatchTest.cs b/MoreLinq.Test/BatchTest.cs index ee10acfb2..f755bc114 100644 --- a/MoreLinq.Test/BatchTest.cs +++ b/MoreLinq.Test/BatchTest.cs @@ -360,7 +360,7 @@ public void BatchBucketSelectorCurrentList() using var pool = new TestArrayPool(); int[]? bucketSelectorItems = null; - var result = input.Batch(4, pool, current => bucketSelectorItems = current.ToArray(), _ => 0); + var result = input.Batch(4, pool, current => bucketSelectorItems = [..current], _ => 0); using var reader = result.Read(); _ = reader.Read(); diff --git a/MoreLinq.Test/BreakingCollection.cs b/MoreLinq.Test/BreakingCollection.cs index 6cb4d03ed..8dda669cb 100644 --- a/MoreLinq.Test/BreakingCollection.cs +++ b/MoreLinq.Test/BreakingCollection.cs @@ -20,12 +20,12 @@ namespace MoreLinq.Test using System; using System.Collections.Generic; - class BreakingCollection : BreakingSequence, ICollection + class BreakingCollection(IList list) : + BreakingSequence, ICollection { - protected readonly IList List; + protected readonly IList List = list; public BreakingCollection(params T[] values) : this((IList)values) { } - public BreakingCollection(IList list) => List = list; public int Count => List.Count; diff --git a/MoreLinq.Test/BreakingList.cs b/MoreLinq.Test/BreakingList.cs index 5f054aba2..32673c6aa 100644 --- a/MoreLinq.Test/BreakingList.cs +++ b/MoreLinq.Test/BreakingList.cs @@ -28,10 +28,11 @@ namespace MoreLinq.Test /// expected to be lazily evaluated. /// - sealed class BreakingList : BreakingCollection, IList + sealed class BreakingList(List list) : + BreakingCollection(list), + IList { - public BreakingList() : this(new List()) { } - public BreakingList(List list) : base(list) { } + public BreakingList() : this([]) { } public int IndexOf(T item) => List.IndexOf(item); public void Insert(int index, T item) => throw new NotImplementedException(); diff --git a/MoreLinq.Test/BreakingReadOnlyCollection.cs b/MoreLinq.Test/BreakingReadOnlyCollection.cs index 9748e77ba..927a9f608 100644 --- a/MoreLinq.Test/BreakingReadOnlyCollection.cs +++ b/MoreLinq.Test/BreakingReadOnlyCollection.cs @@ -19,12 +19,13 @@ namespace MoreLinq.Test { using System.Collections.Generic; - class BreakingReadOnlyCollection : BreakingSequence, IReadOnlyCollection + class BreakingReadOnlyCollection(IReadOnlyCollection collection) : + BreakingSequence, IReadOnlyCollection { - readonly IReadOnlyCollection _collection; + readonly IReadOnlyCollection _collection = collection; public BreakingReadOnlyCollection(params T[] values) : this((IReadOnlyCollection)values) { } - public BreakingReadOnlyCollection(IReadOnlyCollection collection) => _collection = collection; + public int Count => _collection.Count; } } diff --git a/MoreLinq.Test/BreakingReadOnlyList.cs b/MoreLinq.Test/BreakingReadOnlyList.cs index 0e368c8aa..45c84963c 100644 --- a/MoreLinq.Test/BreakingReadOnlyList.cs +++ b/MoreLinq.Test/BreakingReadOnlyList.cs @@ -27,13 +27,13 @@ namespace MoreLinq.Test /// expected to be lazily evaluated. /// - sealed class BreakingReadOnlyList : BreakingReadOnlyCollection, IReadOnlyList + sealed class BreakingReadOnlyList(IReadOnlyList list) : + BreakingReadOnlyCollection(list), + IReadOnlyList { - readonly IReadOnlyList _list; + readonly IReadOnlyList _list = list; public BreakingReadOnlyList(params T[] values) : this((IReadOnlyList)values) { } - public BreakingReadOnlyList(IReadOnlyList list) : base(list) - => _list = list; public T this[int index] => _list[index]; } diff --git a/MoreLinq.Test/CountDownTest.cs b/MoreLinq.Test/CountDownTest.cs index b7c635667..1be4c6846 100644 --- a/MoreLinq.Test/CountDownTest.cs +++ b/MoreLinq.Test/CountDownTest.cs @@ -44,15 +44,15 @@ public void WithNegativeCount() static IEnumerable GetData(Func selector) { var xs = Enumerable.Range(0, 5).ToArray(); - yield return selector(xs, -1, new int?[] { null, null, null, null, null }); - yield return selector(xs, 0, new int?[] { null, null, null, null, null }); - yield return selector(xs, 1, new int?[] { null, null, null, null, 0 }); - yield return selector(xs, 2, new int?[] { null, null, null, 1, 0 }); - yield return selector(xs, 3, new int?[] { null, null, 2, 1, 0 }); - yield return selector(xs, 4, new int?[] { null, 3, 2, 1, 0 }); - yield return selector(xs, 5, new int?[] { 4, 3, 2, 1, 0 }); - yield return selector(xs, 6, new int?[] { 4, 3, 2, 1, 0 }); - yield return selector(xs, 7, new int?[] { 4, 3, 2, 1, 0 }); + yield return selector(xs, -1, [null, null, null, null, null]); + yield return selector(xs, 0, [null, null, null, null, null]); + yield return selector(xs, 1, [null, null, null, null, 0]); + yield return selector(xs, 2, [null, null, null, 1, 0]); + yield return selector(xs, 3, [null, null, 2, 1, 0]); + yield return selector(xs, 4, [null, 3, 2, 1, 0]); + yield return selector(xs, 5, [4, 3, 2, 1, 0]); + yield return selector(xs, 6, [4, 3, 2, 1, 0]); + yield return selector(xs, 7, [4, 3, 2, 1, 0]); } static readonly IEnumerable SequenceData = @@ -170,14 +170,11 @@ public IEnumerator GetEnumerator() => /// enumerator to be substituted for another. /// - sealed class Collection : Sequence, ICollection + sealed class Collection(ICollection collection, + Func, IEnumerator>? em = null) : + Sequence(em), ICollection { - readonly ICollection _collection; - - public Collection(ICollection collection, - Func, IEnumerator>? em = null) : - base(em) => - _collection = collection ?? throw new ArgumentNullException(nameof(collection)); + readonly ICollection _collection = collection ?? throw new ArgumentNullException(nameof(collection)); public int Count => _collection.Count; public bool IsReadOnly => _collection.IsReadOnly; @@ -197,14 +194,11 @@ public Collection(ICollection collection, /// also permits its enumerator to be substituted for another. /// - sealed class ReadOnlyCollection : Sequence, IReadOnlyCollection + sealed class ReadOnlyCollection(ICollection collection, + Func, IEnumerator>? em = null) : + Sequence(em), IReadOnlyCollection { - readonly ICollection _collection; - - public ReadOnlyCollection(ICollection collection, - Func, IEnumerator>? em = null) : - base(em) => - _collection = collection ?? throw new ArgumentNullException(nameof(collection)); + readonly ICollection _collection = collection ?? throw new ArgumentNullException(nameof(collection)); public int Count => _collection.Count; diff --git a/MoreLinq.Test/DistinctByTest.cs b/MoreLinq.Test/DistinctByTest.cs index 70c684592..d78c4614e 100644 --- a/MoreLinq.Test/DistinctByTest.cs +++ b/MoreLinq.Test/DistinctByTest.cs @@ -27,7 +27,7 @@ public class DistinctByTest [Test] public void DistinctBy() { - string[] source = { "first", "second", "third", "fourth", "fifth" }; + string[] source = ["first", "second", "third", "fourth", "fifth"]; var distinct = source.DistinctBy(word => word.Length); distinct.AssertSequenceEqual("first", "second"); } @@ -41,7 +41,7 @@ public void DistinctByIsLazy() [Test] public void DistinctByWithComparer() { - string[] source = { "first", "FIRST", "second", "second", "third" }; + string[] source = ["first", "FIRST", "second", "second", "third"]; var distinct = source.DistinctBy(word => word, StringComparer.OrdinalIgnoreCase); distinct.AssertSequenceEqual("first", "second", "third"); } @@ -49,7 +49,7 @@ public void DistinctByWithComparer() [Test] public void DistinctByNullComparer() { - string[] source = { "first", "second", "third", "fourth", "fifth" }; + string[] source = ["first", "second", "third", "fourth", "fifth"]; var distinct = source.DistinctBy(word => word.Length, null); distinct.AssertSequenceEqual("first", "second"); } diff --git a/MoreLinq.Test/ExceptByTest.cs b/MoreLinq.Test/ExceptByTest.cs index 43c3fa1a1..1935e0793 100644 --- a/MoreLinq.Test/ExceptByTest.cs +++ b/MoreLinq.Test/ExceptByTest.cs @@ -26,8 +26,8 @@ public class ExceptByTest [Test] public void SimpleExceptBy() { - string[] first = { "aaa", "bb", "c", "dddd" }; - string[] second = { "xx", "y" }; + string[] first = ["aaa", "bb", "c", "dddd"]; + string[] second = ["xx", "y"]; var result = first.ExceptBy(second, x => x.Length); result.AssertSequenceEqual("aaa", "dddd"); } @@ -42,8 +42,8 @@ public void ExceptByIsLazy() [Test] public void ExceptByDoesNotRepeatSourceElementsWithDuplicateKeys() { - string[] first = { "aaa", "bb", "c", "a", "b", "c", "dddd" }; - string[] second = { "xx" }; + string[] first = ["aaa", "bb", "c", "a", "b", "c", "dddd"]; + string[] second = ["xx"]; var result = first.ExceptBy(second, x => x.Length); result.AssertSequenceEqual("aaa", "c", "dddd"); } @@ -51,8 +51,8 @@ public void ExceptByDoesNotRepeatSourceElementsWithDuplicateKeys() [Test] public void ExceptByWithComparer() { - string[] first = { "first", "second", "third", "fourth" }; - string[] second = { "FIRST", "thiRD", "FIFTH" }; + string[] first = ["first", "second", "third", "fourth"]; + string[] second = ["FIRST", "thiRD", "FIFTH"]; var result = first.ExceptBy(second, word => word, StringComparer.OrdinalIgnoreCase); result.AssertSequenceEqual("second", "fourth"); } @@ -60,8 +60,8 @@ public void ExceptByWithComparer() [Test] public void ExceptByNullComparer() { - string[] first = { "aaa", "bb", "c", "dddd" }; - string[] second = { "xx", "y" }; + string[] first = ["aaa", "bb", "c", "dddd"]; + string[] second = ["xx", "y"]; var result = first.ExceptBy(second, x => x.Length, null); result.AssertSequenceEqual("aaa", "dddd"); } diff --git a/MoreLinq.Test/FlattenTest.cs b/MoreLinq.Test/FlattenTest.cs index f08a8f6fb..0ad6fd3a8 100644 --- a/MoreLinq.Test/FlattenTest.cs +++ b/MoreLinq.Test/FlattenTest.cs @@ -309,19 +309,19 @@ public void FlattenSelector() new Series { Name = "series1", - Attributes = new[] - { - new Attribute { Values = new[] { 1, 2 } }, - new Attribute { Values = new[] { 3, 4 } }, - } + Attributes = + [ + new Attribute { Values = [1, 2] }, + new Attribute { Values = [3, 4] }, + ] }, new Series { Name = "series2", - Attributes = new[] - { - new Attribute { Values = new[] { 5, 6 } }, - } + Attributes = + [ + new Attribute { Values = [5, 6] }, + ] } }; @@ -416,19 +416,9 @@ sealed class Attribute public required int[] Values; } - sealed class Tree + sealed record Tree(Tree? Left, T Value, Tree? Right) { - public readonly T Value; - public readonly Tree? Left; - public readonly Tree? Right; - public Tree(T value) : this(null, value, null) { } - public Tree(Tree? left, T value, Tree? right) - { - Left = left; - Value = value; - Right = right; - } } } } diff --git a/MoreLinq.Test/GroupAdjacentTest.cs b/MoreLinq.Test/GroupAdjacentTest.cs index b3830a2d2..223d60474 100644 --- a/MoreLinq.Test/GroupAdjacentTest.cs +++ b/MoreLinq.Test/GroupAdjacentTest.cs @@ -207,7 +207,7 @@ public void GroupAdjacentSourceSequenceWithSomeNullKeys() .SelectMany(x => Enumerable.Repeat((int?)x, x).Append(null)) .GroupAdjacent(x => x); - int?[] aNull = { null }; + int?[] aNull = [null]; using var reader = groupings.Read(); AssertGrouping(reader, 1, 1); diff --git a/MoreLinq.Test/PermutationsTest.cs b/MoreLinq.Test/PermutationsTest.cs index 892d3ea9b..44571c6fa 100644 --- a/MoreLinq.Test/PermutationsTest.cs +++ b/MoreLinq.Test/PermutationsTest.cs @@ -78,15 +78,15 @@ public void TestCardinalityThreePermutation() var set = new[] { 42, 11, 100 }; var permutations = set.Permutations(); - var expectedPermutations = new[] - { - new[] {42, 11, 100}, - new[] {42, 100, 11}, - new[] {11, 100, 42}, - new[] {11, 42, 100}, - new[] {100, 11, 42}, - new[] {100, 42, 11}, - }; + var expectedPermutations = new int[][] + { + [42, 11, 100], + [42, 100, 11], + [11, 100, 42], + [11, 42, 100], + [100, 11, 42], + [100, 42, 11], + }; // should contain six permutations (as defined above) Assert.That(permutations.Count(), Is.EqualTo(expectedPermutations.Length)); @@ -103,33 +103,33 @@ public void TestCardinalityFourPermutation() var set = new[] { 42, 11, 100, 89 }; var permutations = set.Permutations(); - var expectedPermutations = new[] - { - new[] {42, 11, 100, 89}, - new[] {42, 100, 11, 89}, - new[] {11, 100, 42, 89}, - new[] {11, 42, 100, 89}, - new[] {100, 11, 42, 89}, - new[] {100, 42, 11, 89}, - new[] {42, 11, 89, 100}, - new[] {42, 100, 89, 11}, - new[] {11, 100, 89, 42}, - new[] {11, 42, 89, 100}, - new[] {100, 11, 89, 42}, - new[] {100, 42, 89, 11}, - new[] {42, 89, 11, 100}, - new[] {42, 89, 100, 11}, - new[] {11, 89, 100, 42}, - new[] {11, 89, 42, 100}, - new[] {100, 89, 11, 42}, - new[] {100, 89, 42, 11}, - new[] {89, 42, 11, 100}, - new[] {89, 42, 100, 11}, - new[] {89, 11, 100, 42}, - new[] {89, 11, 42, 100}, - new[] {89, 100, 11, 42}, - new[] {89, 100, 42, 11}, - }; + var expectedPermutations = new int[][] + { + [42, 11, 100, 89], + [42, 100, 11, 89], + [11, 100, 42, 89], + [11, 42, 100, 89], + [100, 11, 42, 89], + [100, 42, 11, 89], + [42, 11, 89, 100], + [42, 100, 89, 11], + [11, 100, 89, 42], + [11, 42, 89, 100], + [100, 11, 89, 42], + [100, 42, 89, 11], + [42, 89, 11, 100], + [42, 89, 100, 11], + [11, 89, 100, 42], + [11, 89, 42, 100], + [100, 89, 11, 42], + [100, 89, 42, 11], + [89, 42, 11, 100], + [89, 42, 100, 11], + [89, 11, 100, 42], + [89, 11, 42, 100], + [89, 100, 11, 42], + [89, 100, 42, 11], + }; // should contain six permutations (as defined above) Assert.That(permutations.Count(), Is.EqualTo(expectedPermutations.Length)); diff --git a/MoreLinq.Test/PrependTest.cs b/MoreLinq.Test/PrependTest.cs index de8253f6e..00d5bf982 100644 --- a/MoreLinq.Test/PrependTest.cs +++ b/MoreLinq.Test/PrependTest.cs @@ -28,7 +28,7 @@ public class PrependTest [Test] public void PrependWithNonEmptyTailSequence() { - string[] tail = { "second", "third" }; + string[] tail = ["second", "third"]; var head = "first"; var whole = tail.Prepend(head); whole.AssertSequenceEqual("first", "second", "third"); @@ -37,7 +37,7 @@ public void PrependWithNonEmptyTailSequence() [Test] public void PrependWithEmptyTailSequence() { - string[] tail = { }; + string[] tail = []; var head = "first"; var whole = tail.Prepend(head); whole.AssertSequenceEqual("first"); @@ -46,7 +46,7 @@ public void PrependWithEmptyTailSequence() [Test] public void PrependWithNullHead() { - string[] tail = { "second", "third" }; + string[] tail = ["second", "third"]; string? head = null; var whole = tail.Prepend(head); whole.AssertSequenceEqual(null, "second", "third"); diff --git a/MoreLinq.Test/ReadOnlyCollection.cs b/MoreLinq.Test/ReadOnlyCollection.cs index 23da7f290..82004faf4 100644 --- a/MoreLinq.Test/ReadOnlyCollection.cs +++ b/MoreLinq.Test/ReadOnlyCollection.cs @@ -25,12 +25,11 @@ static class ReadOnlyCollection public static IReadOnlyCollection From(params T[] items) => new ListCollection(items); - sealed class ListCollection : IReadOnlyCollection + sealed class ListCollection(TList list) : + IReadOnlyCollection where TList : IList { - readonly TList _list; - - public ListCollection(TList list) => _list = list; + readonly TList _list = list; public IEnumerator GetEnumerator() => _list.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); diff --git a/MoreLinq.Test/SequenceReader.cs b/MoreLinq.Test/SequenceReader.cs index 5a2a0cba6..c49c51c3b 100644 --- a/MoreLinq.Test/SequenceReader.cs +++ b/MoreLinq.Test/SequenceReader.cs @@ -35,9 +35,14 @@ public static SequenceReader Read(this IEnumerable source) /// "read" operation. /// /// Type of elements to read. - sealed class SequenceReader : IDisposable + /// + /// Initializes a instance + /// from an enumerator. + /// + /// Source enumerator. + sealed class SequenceReader(IEnumerator enumerator) : IDisposable { - IEnumerator? _enumerator; + IEnumerator? _enumerator = enumerator ?? throw new ArgumentNullException(nameof(enumerator)); /// /// Initializes a instance @@ -48,15 +53,6 @@ sealed class SequenceReader : IDisposable public SequenceReader(IEnumerable source) : this(GetEnumerator(source)) { } - /// - /// Initializes a instance - /// from an enumerator. - /// - /// Source enumerator. - - public SequenceReader(IEnumerator enumerator) => - _enumerator = enumerator ?? throw new ArgumentNullException(nameof(enumerator)); - static IEnumerator GetEnumerator(IEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); diff --git a/MoreLinq.Test/SubsetTest.cs b/MoreLinq.Test/SubsetTest.cs index 5b35524bf..ef079e166 100644 --- a/MoreLinq.Test/SubsetTest.cs +++ b/MoreLinq.Test/SubsetTest.cs @@ -117,14 +117,14 @@ public void TestAllSubsetsExpectedResults() var sequence = Enumerable.Range(1, 4); var result = sequence.Subsets(); - var expectedSubsets = new[] - { - new int[] {}, - new[] {1}, new[] {2}, new[] {3}, new[] {4}, - new[] {1,2}, new[] {1,3}, new[] {1,4}, new[] {2,3}, new[] {2,4}, new[] {3,4}, - new[] {1,2,3}, new[] {1,2,4}, new[] {1,3,4}, new[] {2,3,4}, - new[] {1,2,3,4} - }; + var expectedSubsets = new int[][] + { + [], + [1], [2], [3], [4], + [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], + [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], + [1, 2, 3, 4] + }; var index = 0; foreach (var subset in result) @@ -170,24 +170,24 @@ public void TestKSubsetExpectedResult() var sequence = Enumerable.Range(1, 6); var result = sequence.Subsets(4); - var expectedSubsets = new[] - { - new[] {1,2,3,4}, - new[] {1,2,3,5}, - new[] {1,2,3,6}, - new[] {1,2,4,5}, - new[] {1,2,4,6}, - new[] {1,2,5,6}, - new[] {1,3,4,5}, - new[] {1,3,4,6}, - new[] {1,3,5,6}, - new[] {1,4,5,6}, - new[] {2,3,4,5}, - new[] {2,3,4,6}, - new[] {2,3,5,6}, - new[] {2,4,5,6}, - new[] {3,4,5,6}, - }; + var expectedSubsets = new int[][] + { + [1, 2, 3, 4], + [1, 2, 3, 5], + [1, 2, 3, 6], + [1, 2, 4, 5], + [1, 2, 4, 6], + [1, 2, 5, 6], + [1, 3, 4, 5], + [1, 3, 4, 6], + [1, 3, 5, 6], + [1, 4, 5, 6], + [2, 3, 4, 5], + [2, 3, 4, 6], + [2, 3, 5, 6], + [2, 4, 5, 6], + [3, 4, 5, 6], + }; var index = 0; foreach (var subset in result) diff --git a/MoreLinq.Test/TestException.cs b/MoreLinq.Test/TestException.cs index 98cb7c715..48f0cb740 100644 --- a/MoreLinq.Test/TestException.cs +++ b/MoreLinq.Test/TestException.cs @@ -21,9 +21,8 @@ namespace MoreLinq.Test /// Reserved for use within tests. /// - sealed class TestException : System.Exception + sealed class TestException(string? message) : System.Exception(message) { public TestException() : this(null) { } - public TestException(string? message) : base(message) { } } } diff --git a/MoreLinq.Test/ToDataTableTest.cs b/MoreLinq.Test/ToDataTableTest.cs index ae053830c..5842fe23a 100644 --- a/MoreLinq.Test/ToDataTableTest.cs +++ b/MoreLinq.Test/ToDataTableTest.cs @@ -27,26 +27,17 @@ namespace MoreLinq.Test [TestFixture] public class ToDataTableTest { - sealed class TestObject + sealed class TestObject(int key) { - public int KeyField; - public Guid? ANullableGuidField; + public int KeyField = key; + public Guid? ANullableGuidField = Guid.NewGuid(); - public string AString { get; } - public decimal? ANullableDecimal { get; } + public string AString { get; } = "ABCDEFGHIKKLMNOPQRSTUVWXYSZ"; + public decimal? ANullableDecimal { get; } = key / 3; public object Unreadable { set => throw new NotImplementedException(); } public object this[int index] { get => new(); set { } } - public TestObject(int key) - { - KeyField = key; - ANullableGuidField = Guid.NewGuid(); - - ANullableDecimal = key / 3; - AString = "ABCDEFGHIKKLMNOPQRSTUVWXYSZ"; - } - public override string ToString() => nameof(TestObject); } diff --git a/MoreLinq.Test/TransposeTest.cs b/MoreLinq.Test/TransposeTest.cs index 826e55687..2e1cf417d 100644 --- a/MoreLinq.Test/TransposeTest.cs +++ b/MoreLinq.Test/TransposeTest.cs @@ -45,12 +45,12 @@ public void TransposeWithOneNullRow() [Test] public void TransposeWithRowsOfSameLength() { - var expectations = new[] + var expectations = new int[][] { - new [] { 10, 20, 30 }, - new [] { 11, 21, 31 }, - new [] { 12, 22, 32 }, - new [] { 13, 23, 33 }, + [10, 20, 30], + [11, 21, 31], + [12, 22, 32], + [13, 23, 33], }; using var row1 = TestingSequence.Of(10, 11, 12, 13); @@ -64,11 +64,11 @@ public void TransposeWithRowsOfSameLength() [Test] public void TransposeWithRowsOfDifferentLengths() { - var expectations = new[] + var expectations = new int[][] { - new[] { 10, 20, 30 }, - new[] { 11, 31 }, - new[] { 32 } + [10, 20, 30], + [11, 31], + [32] }; using var row1 = TestingSequence.Of(10, 11); @@ -85,10 +85,10 @@ public void TransposeMaintainsCornerElements() { var matrix = new[] { - new[] { 10, 11 }, - new[] { 20 }, + [10, 11], + [20], new int[0], - new[] { 30, 31, 32 } + [30, 31, 32] }; var traspose = matrix.Transpose(); @@ -107,13 +107,13 @@ public void TransposeWithAllRowsAsInfiniteSequences() var result = matrix.Transpose().Take(5); - var expectations = new[] + var expectations = new int[][] { - new[] { 2, 3, 5 }, - new[] { 4, 9, 25 }, - new[] { 8, 27, 125 }, - new[] { 16, 81, 625 }, - new[] { 32, 243, 3125 } + [2, 3, 5], + [4, 9, 25], + [8, 27, 125], + [16, 81, 625], + [32, 243, 3125] }; AssertMatrix(expectations, result); @@ -130,13 +130,13 @@ public void TransposeWithSomeRowsAsInfiniteSequences() var result = matrix.Transpose().Take(5); - var expectations = new[] + var expectations = new int[][] { - new[] { 2, 3, 5 }, - new[] { 4, 9, 25 }, - new[] { 8, 125 }, - new[] { 16, 625 }, - new[] { 32, 3125 } + [2, 3, 5], + [4, 9, 25], + [8, 125], + [16, 625], + [32, 3125] }; AssertMatrix(expectations, result); @@ -147,10 +147,10 @@ public void TransposeColumnTraversalOrderIsIrrelevant() { var matrix = new[] { - new[] { 10, 11 }, - new[] { 20 }, + [10, 11], + [20], new int[0], - new[] { 30, 31, 32 } + [30, 31, 32] }; var transpose = matrix.Transpose().ToList(); diff --git a/MoreLinq.Test/TraverseTest.cs b/MoreLinq.Test/TraverseTest.cs index 163b3b401..89a0b2277 100644 --- a/MoreLinq.Test/TraverseTest.cs +++ b/MoreLinq.Test/TraverseTest.cs @@ -50,16 +50,10 @@ public void TraverseBreadthFirstPreservesChildrenOrder() res.AssertSequenceEqual(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } - sealed class Tree + sealed class Tree(T value, IEnumerable> children) { - public T Value { get; } - public IEnumerable> Children { get; } - - public Tree(T value, IEnumerable> children) - { - Value = value; - Children = children; - } + public T Value { get; } = value; + public IEnumerable> Children { get; } = children; } static class Tree diff --git a/MoreLinq.Test/TrySingleTest.cs b/MoreLinq.Test/TrySingleTest.cs index 45186c623..01b12b313 100644 --- a/MoreLinq.Test/TrySingleTest.cs +++ b/MoreLinq.Test/TrySingleTest.cs @@ -65,10 +65,10 @@ public void TrySingleWithSingletonCollection(IEnumerable source, T result) } static readonly ITestCaseData[] SingletonCollectionTestCases = - { + [ new TestCaseData(new BreakingSingleElementCollection(10), 10), new TestCaseData(new BreakingSingleElementReadOnlyCollection(20), 20) - }; + ]; class BreakingSingleElementCollectionBase : IEnumerable { @@ -89,11 +89,10 @@ public IEnumerator GetEnumerator() IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } - sealed class BreakingSingleElementCollection : - BreakingSingleElementCollectionBase, ICollection + sealed class BreakingSingleElementCollection(T element) : + BreakingSingleElementCollectionBase(element), + ICollection { - public BreakingSingleElementCollection(T element) : base(element) { } - public void Add(T item) => throw new NotImplementedException(); public void Clear() => throw new NotImplementedException(); public bool Contains(T item) => throw new NotImplementedException(); @@ -102,10 +101,9 @@ public BreakingSingleElementCollection(T element) : base(element) { } public bool IsReadOnly => true; } - sealed class BreakingSingleElementReadOnlyCollection : - BreakingSingleElementCollectionBase, IReadOnlyCollection + sealed class BreakingSingleElementReadOnlyCollection(T element) : + BreakingSingleElementCollectionBase(element), IReadOnlyCollection { - public BreakingSingleElementReadOnlyCollection(T element) : base(element) { } } [TestCase(SourceKind.Sequence)] diff --git a/MoreLinq.Test/WatchableEnumerator.cs b/MoreLinq.Test/WatchableEnumerator.cs index 4cb3d4221..2d782604d 100644 --- a/MoreLinq.Test/WatchableEnumerator.cs +++ b/MoreLinq.Test/WatchableEnumerator.cs @@ -26,17 +26,15 @@ partial class TestExtensions public static WatchableEnumerator AsWatchable(this IEnumerator source) => new(source); } - sealed class WatchableEnumerator : IEnumerator + sealed class WatchableEnumerator(IEnumerator source) : + IEnumerator { - readonly IEnumerator _source; + readonly IEnumerator _source = source ?? throw new ArgumentNullException(nameof(source)); public event EventHandler? Disposed; public event EventHandler? GetCurrentCalled; public event EventHandler? MoveNextCalled; - public WatchableEnumerator(IEnumerator source) => - _source = source ?? throw new ArgumentNullException(nameof(source)); - public T Current { get diff --git a/MoreLinq/Acquire.cs b/MoreLinq/Acquire.cs index d7e44900f..6b3d47121 100644 --- a/MoreLinq/Acquire.cs +++ b/MoreLinq/Acquire.cs @@ -47,7 +47,7 @@ public static TSource[] Acquire(this IEnumerable source) try { disposables.AddRange(source); - return disposables.ToArray(); + return [..disposables]; } catch { diff --git a/MoreLinq/Collections/Dictionary.cs b/MoreLinq/Collections/Dictionary.cs index a325768d8..f79b23104 100644 --- a/MoreLinq/Collections/Dictionary.cs +++ b/MoreLinq/Collections/Dictionary.cs @@ -49,13 +49,11 @@ public TValue this[TKey key] public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) => _dict.TryGetValue(ValueTuple.Create(key), out value); - sealed class ValueTupleItemComparer : IEqualityComparer> + sealed class ValueTupleItemComparer(IEqualityComparer comparer) : + IEqualityComparer> { - readonly IEqualityComparer _comparer; - - public ValueTupleItemComparer(IEqualityComparer comparer) => _comparer = comparer; - public bool Equals(ValueTuple x, ValueTuple y) => _comparer.Equals(x.Item1, y.Item1); - public int GetHashCode(ValueTuple obj) => obj.Item1 is { } some ? _comparer.GetHashCode(some) : 0; + public bool Equals(ValueTuple x, ValueTuple y) => comparer.Equals(x.Item1, y.Item1); + public int GetHashCode(ValueTuple obj) => obj.Item1 is { } some ? comparer.GetHashCode(some) : 0; } } } diff --git a/MoreLinq/CountBy.cs b/MoreLinq/CountBy.cs index 282ace5d4..c1f8873ed 100644 --- a/MoreLinq/CountBy.cs +++ b/MoreLinq/CountBy.cs @@ -80,8 +80,8 @@ void Loop(IEqualityComparer cmp) { var dic = new Collections.Dictionary(cmp); - keys = new List(); - counts = new List(); + keys = []; + counts = []; foreach (var item in source) { diff --git a/MoreLinq/Delegating.cs b/MoreLinq/Delegating.cs index 9b427e6c5..81c9ada81 100644 --- a/MoreLinq/Delegating.cs +++ b/MoreLinq/Delegating.cs @@ -40,12 +40,9 @@ public static IObserver Observer(Action onNext, new DelegatingObserver(onNext, onError, onCompleted); } - sealed class DelegatingDisposable : IDisposable + sealed class DelegatingDisposable(Action delegatee) : IDisposable { - Action? _delegatee; - - public DelegatingDisposable(Action delegatee) => - _delegatee = delegatee ?? throw new ArgumentNullException(nameof(delegatee)); + Action? _delegatee = delegatee ?? throw new ArgumentNullException(nameof(delegatee)); public void Dispose() { @@ -56,23 +53,15 @@ public void Dispose() } } - sealed class DelegatingObserver : IObserver + sealed class DelegatingObserver(Action onNext, + Action? onError = null, + Action? onCompleted = null) : + IObserver { - readonly Action _onNext; - readonly Action? _onError; - readonly Action? _onCompleted; - - public DelegatingObserver(Action onNext, - Action? onError = null, - Action? onCompleted = null) - { - _onNext = onNext ?? throw new ArgumentNullException(nameof(onNext)); - _onError = onError; - _onCompleted = onCompleted; - } + readonly Action _onNext = onNext ?? throw new ArgumentNullException(nameof(onNext)); - public void OnCompleted() => _onCompleted?.Invoke(); - public void OnError(Exception error) => _onError?.Invoke(error); + public void OnCompleted() => onCompleted?.Invoke(); + public void OnError(Exception error) => onError?.Invoke(error); public void OnNext(T value) => _onNext(value); } } diff --git a/MoreLinq/EmptyArray.cs b/MoreLinq/EmptyArray.cs deleted file mode 100644 index fa49b3567..000000000 --- a/MoreLinq/EmptyArray.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace MoreLinq -{ - static class EmptyArray - { - public static readonly T[] Value = -#if NETSTANDARD1_6_OR_GREATER || NET6_0_OR_GREATER - System.Array.Empty(); -#else -#pragma warning disable CA1825 // Avoid zero-length array allocations - new T[0]; -#pragma warning restore CA1825 // Avoid zero-length array allocations -#endif - } -} diff --git a/MoreLinq/EquiZip.cs b/MoreLinq/EquiZip.cs index 61212f122..7417e17e2 100644 --- a/MoreLinq/EquiZip.cs +++ b/MoreLinq/EquiZip.cs @@ -201,7 +201,7 @@ static IEnumerable EquiZipImpl( } static readonly string[] OrdinalNumbers = - { + [ "First", "Second", "Third", @@ -218,6 +218,6 @@ static IEnumerable EquiZipImpl( // "Fourteenth", // "Fifteenth", // "Sixteenth", - }; + ]; } } diff --git a/MoreLinq/Experimental/Async/Merge.cs b/MoreLinq/Experimental/Async/Merge.cs index 7e6312b9b..72c9a459c 100644 --- a/MoreLinq/Experimental/Async/Merge.cs +++ b/MoreLinq/Experimental/Async/Merge.cs @@ -107,7 +107,7 @@ async IAsyncEnumerable Async([EnumeratorCancellation]CancellationToken cancel enumeratorList.AddRange(from s in sources select s.GetAsyncEnumerator(cancellationToken)); - pendingTaskList = new List)>>(); + pendingTaskList = []; const bool some = true; @@ -117,7 +117,7 @@ async IAsyncEnumerable Async([EnumeratorCancellation]CancellationToken cancel var disposalTask = enumerator.DisposeAsync(); if (disposalTask.IsCompleted) return disposalTask; - disposalTaskList ??= new List(); + disposalTaskList ??= []; disposalTaskList.Add(disposalTask.AsTask()); return null; } @@ -232,7 +232,7 @@ async IAsyncEnumerable Async([EnumeratorCancellation]CancellationToken cancel } else { - disposalTaskList ??= new List(); + disposalTaskList ??= []; disposalTaskList.Add(task.AsTask()); } } diff --git a/MoreLinq/Experimental/Await.cs b/MoreLinq/Experimental/Await.cs index abbfa0caa..c7d1919f6 100644 --- a/MoreLinq/Experimental/Await.cs +++ b/MoreLinq/Experimental/Await.cs @@ -705,26 +705,18 @@ public static IAwaitQuery new AwaitQuery(impl, options); } - sealed class AwaitQuery : IAwaitQuery + sealed class AwaitQuery(Func> impl, + AwaitQueryOptions? options = null) : IAwaitQuery { - readonly Func> _impl; - - public AwaitQuery(Func> impl, - AwaitQueryOptions? options = null) - { - _impl = impl; - Options = options ?? AwaitQueryOptions.Default; - } - - public AwaitQueryOptions Options { get; } + public AwaitQueryOptions Options { get; } = options ?? AwaitQueryOptions.Default; public IAwaitQuery WithOptions(AwaitQueryOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - return Options == options ? this : new AwaitQuery(_impl, options); + return Options == options ? this : new AwaitQuery(impl, options); } - public IEnumerator GetEnumerator() => _impl(Options).GetEnumerator(); + public IEnumerator GetEnumerator() => impl(Options).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } diff --git a/MoreLinq/Experimental/Batch.cs b/MoreLinq/Experimental/Batch.cs index ee25e1619..f4cae14be 100644 --- a/MoreLinq/Experimental/Batch.cs +++ b/MoreLinq/Experimental/Batch.cs @@ -229,7 +229,7 @@ ICurrentBufferProvider Cursor(IEnumerator<(T[], int)> source) => sealed class CurrentPoolArrayProvider : CurrentBuffer, ICurrentBufferProvider { bool _rented; - T[] _array = Array.Empty(); + T[] _array = []; int _count; IEnumerator<(T[], int)>? _rental; ArrayPool? _pool; @@ -280,7 +280,7 @@ public void Dispose() if (_rented) _pool.Return(array); enumerator.Dispose(); - _array = Array.Empty(); + _array = []; _count = 0; _rental = null; _pool = null; diff --git a/MoreLinq/Experimental/Memoize.cs b/MoreLinq/Experimental/Memoize.cs index 258037e9c..d7ee0f7ab 100644 --- a/MoreLinq/Experimental/Memoize.cs +++ b/MoreLinq/Experimental/Memoize.cs @@ -62,21 +62,15 @@ public static IEnumerable Memoize(this IEnumerable source) => }; } - sealed class MemoizedEnumerable : IEnumerable, IDisposable + sealed class MemoizedEnumerable(IEnumerable sequence) : IEnumerable, IDisposable { List? _cache; - readonly object _locker; - readonly IEnumerable _source; + readonly object _locker = new(); + readonly IEnumerable _source = sequence ?? throw new ArgumentNullException(nameof(sequence)); IEnumerator? _sourceEnumerator; int? _errorIndex; ExceptionDispatchInfo? _error; - public MemoizedEnumerable(IEnumerable sequence) - { - _source = sequence ?? throw new ArgumentNullException(nameof(sequence)); - _locker = new object(); - } - public IEnumerator GetEnumerator() { if (_cache == null) diff --git a/MoreLinq/FillBackward.cs b/MoreLinq/FillBackward.cs index c07c8a81b..639f1d450 100644 --- a/MoreLinq/FillBackward.cs +++ b/MoreLinq/FillBackward.cs @@ -114,7 +114,7 @@ static IEnumerable FillBackwardImpl(IEnumerable source, Func p var isBlank = predicate(item); if (isBlank) { - (blanks ??= new List()).Add(item); + (blanks ??= []).Add(item); } else { diff --git a/MoreLinq/GroupAdjacent.cs b/MoreLinq/GroupAdjacent.cs index 8cf89a5e3..fe6043f82 100644 --- a/MoreLinq/GroupAdjacent.cs +++ b/MoreLinq/GroupAdjacent.cs @@ -305,19 +305,12 @@ public static Grouping Create(TKey key, IEnumera } [Serializable] - sealed class Grouping : IGrouping + sealed class Grouping(TKey key, IEnumerable members) : + IGrouping { - readonly IEnumerable _members; + public TKey Key { get; } = key; - public Grouping(TKey key, IEnumerable members) - { - Key = key; - _members = members; - } - - public TKey Key { get; } - - public IEnumerator GetEnumerator() => _members.GetEnumerator(); + public IEnumerator GetEnumerator() => members.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } diff --git a/MoreLinq/Maxima.cs b/MoreLinq/Maxima.cs index 60150a433..d5262251b 100644 --- a/MoreLinq/Maxima.cs +++ b/MoreLinq/Maxima.cs @@ -218,21 +218,13 @@ public static IExtremaEnumerable Maxima(this IEnumerable return new ExtremaEnumerable(source, selector, comparer.Compare); } - sealed class ExtremaEnumerable : IExtremaEnumerable + sealed class ExtremaEnumerable(IEnumerable source, + Func selector, + Func comparer) : + IExtremaEnumerable { - readonly IEnumerable _source; - readonly Func _selector; - readonly Func _comparer; - - public ExtremaEnumerable(IEnumerable source, Func selector, Func comparer) - { - _source = source; - _selector = selector; - _comparer = comparer; - } - public IEnumerator GetEnumerator() => - ExtremaBy(_source, Extrema.First, null, _selector, _comparer).GetEnumerator(); + ExtremaBy(source, Extrema.First, null, selector, comparer).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); @@ -241,16 +233,16 @@ public IEnumerable Take(int count) => count switch { 0 => Enumerable.Empty(), - 1 => ExtremaBy(_source, Extremum.First, 1 , _selector, _comparer), - _ => ExtremaBy(_source, Extrema.First , count, _selector, _comparer) + 1 => ExtremaBy(source, Extremum.First, 1 , selector, comparer), + _ => ExtremaBy(source, Extrema.First , count, selector, comparer) }; public IEnumerable TakeLast(int count) => count switch { 0 => Enumerable.Empty(), - 1 => ExtremaBy(_source, Extremum.Last, 1 , _selector, _comparer), - _ => ExtremaBy(_source, Extrema.Last , count, _selector, _comparer) + 1 => ExtremaBy(source, Extremum.Last, 1 , selector, comparer), + _ => ExtremaBy(source, Extrema.Last , count, selector, comparer) }; static class Extrema @@ -267,7 +259,7 @@ sealed class FirstExtrema : Extrema?, T> public override void Add(ref List? store, int? limit, T item) { if (limit == null || store is null || store.Count < limit) - (store ??= new List()).Add(item); + (store ??= []).Add(item); } } diff --git a/MoreLinq/Partition.cs b/MoreLinq/Partition.cs index aa72f8725..9660186fc 100644 --- a/MoreLinq/Partition.cs +++ b/MoreLinq/Partition.cs @@ -367,7 +367,7 @@ static TResult PartitionImpl(IEnumerable>(); + etc ??= []; etc.Add(e); } else diff --git a/MoreLinq/PendNode.cs b/MoreLinq/PendNode.cs index 2bf363863..9c3e4e5f5 100644 --- a/MoreLinq/PendNode.cs +++ b/MoreLinq/PendNode.cs @@ -53,10 +53,9 @@ public Item(T item, bool isPrepend, PendNode next) } } - sealed class Source : PendNode + sealed class Source(IEnumerable source) : PendNode { - public IEnumerable Value { get; } - public Source(IEnumerable source) => Value = source; + public IEnumerable Value { get; } = source; } public IEnumerator GetEnumerator() diff --git a/MoreLinq/Reactive/Subject.cs b/MoreLinq/Reactive/Subject.cs index 145282cf0..40f2ed6d2 100644 --- a/MoreLinq/Reactive/Subject.cs +++ b/MoreLinq/Reactive/Subject.cs @@ -28,7 +28,7 @@ sealed class Subject : IObservable, IObserver Exception? _error; bool HasObservers => (_observers?.Count ?? 0) > 0; - List> Observers => _observers ??= new List>(); + List> Observers => _observers ??= []; bool IsMuted => _completed || _error != null; diff --git a/MoreLinq/Return.cs b/MoreLinq/Return.cs index 35ffa3adc..078d5beb7 100644 --- a/MoreLinq/Return.cs +++ b/MoreLinq/Return.cs @@ -32,11 +32,9 @@ partial class MoreEnumerable public static IEnumerable Return(T item) => new SingleElementList(item); - sealed class SingleElementList : IList, IReadOnlyList + sealed class SingleElementList(T item) : IList, IReadOnlyList { - readonly T _item; - - public SingleElementList(T item) => _item = item; + readonly T _item = item; public int Count => 1; public bool IsReadOnly => true; diff --git a/MoreLinq/ReverseComparer.cs b/MoreLinq/ReverseComparer.cs index 63fbf527f..e80749bcd 100644 --- a/MoreLinq/ReverseComparer.cs +++ b/MoreLinq/ReverseComparer.cs @@ -19,12 +19,9 @@ namespace MoreLinq { using System.Collections.Generic; - sealed class ReverseComparer : IComparer + sealed class ReverseComparer(IComparer? underlying) : IComparer { - readonly IComparer _underlying; - - public ReverseComparer(IComparer? underlying) => - _underlying = underlying ?? Comparer.Default; + readonly IComparer _underlying = underlying ?? Comparer.Default; public int Compare #if NETCOREAPP3_1_OR_GREATER diff --git a/MoreLinq/Segment.cs b/MoreLinq/Segment.cs index 788aa3ae7..859f59014 100644 --- a/MoreLinq/Segment.cs +++ b/MoreLinq/Segment.cs @@ -95,7 +95,7 @@ public static IEnumerable> Segment(this IEnumerable source, if (newSegmentPredicate(current, previous, index)) { yield return segment; // yield the completed segment - segment = new List { current }; // start a new segment + segment = [current]; // start a new segment } else // not a new segment, append and continue { diff --git a/MoreLinq/SequenceException.cs b/MoreLinq/SequenceException.cs index 6c5088984..11ac75b31 100644 --- a/MoreLinq/SequenceException.cs +++ b/MoreLinq/SequenceException.cs @@ -52,7 +52,9 @@ public SequenceException(string? message) : /// A message that describes the error. /// The exception that is the cause of the current exception. +#pragma warning disable IDE0290 // Use primary constructor (needed due to deserialization constructor) public SequenceException(string? message, Exception? innerException) : +#pragma warning restore IDE0290 // Use primary constructor base(string.IsNullOrEmpty(message) ? DefaultMessage : message, innerException) { } } } diff --git a/MoreLinq/SortedMerge.cs b/MoreLinq/SortedMerge.cs index 9f6aff425..03757dcb5 100644 --- a/MoreLinq/SortedMerge.cs +++ b/MoreLinq/SortedMerge.cs @@ -156,12 +156,9 @@ IEnumerable Impl(IEnumerable> sequences) /// are disposed - either when Excluded or when the DisposableGroup is disposed. /// - sealed class DisposableGroup : IDisposable + sealed class DisposableGroup(IEnumerable> iterators) : IDisposable { - public DisposableGroup(IEnumerable> iterators) => - Iterators = new List>(iterators); - - public List> Iterators { get; } + public List> Iterators { get; } = new(iterators); public IEnumerator this[int index] => Iterators[index]; diff --git a/MoreLinq/Split.cs b/MoreLinq/Split.cs index 1e101be66..935537aa5 100644 --- a/MoreLinq/Split.cs +++ b/MoreLinq/Split.cs @@ -287,7 +287,7 @@ public static IEnumerable Split(this IEnumerable(); + items ??= []; items.Add(item); } } diff --git a/MoreLinq/ToArrayByIndex.cs b/MoreLinq/ToArrayByIndex.cs index ce7c5917f..b450d0be5 100644 --- a/MoreLinq/ToArrayByIndex.cs +++ b/MoreLinq/ToArrayByIndex.cs @@ -122,7 +122,7 @@ public static TResult[] ToArrayByIndex(this IEnumerable source, var lastIndex = -1; var indexed = (List>?)null; - List> Indexed() => indexed ??= new List>(); + List> Indexed() => indexed ??= []; foreach (var e in source) { @@ -143,7 +143,7 @@ public static TResult[] ToArrayByIndex(this IEnumerable source, var length = lastIndex + 1; return length == 0 - ? EmptyArray.Value + ? [] : Indexed().ToArrayByIndex(length, e => e.Key, e => resultSelector(e.Value, e.Key)); } diff --git a/MoreLinq/ToDataTable.cs b/MoreLinq/ToDataTable.cs index 08027567c..ba2275956 100644 --- a/MoreLinq/ToDataTable.cs +++ b/MoreLinq/ToDataTable.cs @@ -41,7 +41,7 @@ static partial class MoreEnumerable public static TTable ToDataTable(this IEnumerable source, TTable table) where TTable : DataTable { - return ToDataTable(source, table, EmptyArray>>.Value); + return ToDataTable(source, table, []); } /// diff --git a/bld/ExtensionsGenerator/Program.cs b/bld/ExtensionsGenerator/Program.cs index 762eae7e7..35dae759d 100644 --- a/bld/ExtensionsGenerator/Program.cs +++ b/bld/ExtensionsGenerator/Program.cs @@ -380,9 +380,8 @@ protected static int Compare(IEnumerable a, IEnumerable b) => .FirstOrDefault(e => e != 0); } -sealed class SimpleTypeKey : TypeKey +sealed class SimpleTypeKey(string name) : TypeKey(name) { - public SimpleTypeKey(string name) : base(name) { } public override string ToString() => Name; public override ImmutableList Parameters => ImmutableList.Empty; } @@ -390,7 +389,7 @@ public SimpleTypeKey(string name) : base(name) { } abstract class ParameterizedTypeKey : TypeKey { protected ParameterizedTypeKey(string name, TypeKey parameter) : - this(name, ImmutableList.Create(parameter)) { } + this(name, [parameter]) { } protected ParameterizedTypeKey(string name, ImmutableList parameters) : base(name) => Parameters = parameters; @@ -398,36 +397,30 @@ protected ParameterizedTypeKey(string name, ImmutableList parameters) : public override ImmutableList Parameters { get; } } -sealed class GenericTypeKey : ParameterizedTypeKey +sealed class GenericTypeKey(string name, ImmutableList parameters) : + ParameterizedTypeKey(name, parameters) { - public GenericTypeKey(string name, ImmutableList parameters) : - base(name, parameters) { } - public override string ToString() => Name + "<" + string.Join(", ", Parameters) + ">"; } -sealed class NullableTypeKey : ParameterizedTypeKey +sealed class NullableTypeKey(TypeKey underlying) : + ParameterizedTypeKey("?", underlying) { - public NullableTypeKey(TypeKey underlying) : base("?", underlying) { } public override string ToString() => Parameters.Single() + "?"; } -sealed class TupleTypeKey : ParameterizedTypeKey +sealed class TupleTypeKey(ImmutableList parameters) : + ParameterizedTypeKey("()", parameters) { - public TupleTypeKey(ImmutableList parameters) : - base("()", parameters) { } - public override string ToString() => "(" + string.Join(", ", Parameters) + ")"; } -sealed class ArrayTypeKey : ParameterizedTypeKey +sealed class ArrayTypeKey(TypeKey element, IEnumerable ranks) : + ParameterizedTypeKey("[]", element) { - public ArrayTypeKey(TypeKey element, IEnumerable ranks) : - base("[]", element) => Ranks = ImmutableList.CreateRange(ranks); - - public ImmutableList Ranks { get; } + public ImmutableList Ranks { get; } = ImmutableList.CreateRange(ranks); public override string ToString() => Parameters.Single() + string.Concat(from r in Ranks