Skip to content

Commit 845294d

Browse files
authored
Hide "CountBy" in .NET 9+
This is squashed merge of PR #1092 that closes #1020.
1 parent 90a35c5 commit 845294d

File tree

8 files changed

+756
-15
lines changed

8 files changed

+756
-15
lines changed

MoreLinq.Test/CountByTest.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class CountByTest
2626
[Test]
2727
public void CountBySimpleTest()
2828
{
29-
var result = new[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2 }.CountBy(c => c);
29+
var result = MoreEnumerable.CountBy([1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2 ], c => c);
3030

3131
result.AssertSequenceEqual(
3232
KeyValuePair.Create(1, 4),
@@ -40,7 +40,7 @@ public void CountBySimpleTest()
4040
[Test]
4141
public void CountByWithSecondOccurenceImmediatelyAfterFirst()
4242
{
43-
var result = "jaffer".CountBy(c => c);
43+
var result = MoreEnumerable.CountBy("jaffer", c => c);
4444

4545
result.AssertSequenceEqual(
4646
KeyValuePair.Create('j', 1),
@@ -53,7 +53,7 @@ public void CountByWithSecondOccurenceImmediatelyAfterFirst()
5353
[Test]
5454
public void CountByEvenOddTest()
5555
{
56-
var result = Enumerable.Range(1, 100).CountBy(c => c % 2);
56+
var result = MoreEnumerable.CountBy(Enumerable.Range(1, 100), c => c % 2);
5757

5858
result.AssertSequenceEqual(
5959
KeyValuePair.Create(1, 50),
@@ -63,7 +63,7 @@ public void CountByEvenOddTest()
6363
[Test]
6464
public void CountByWithEqualityComparer()
6565
{
66-
var result = new[] { "a", "B", "c", "A", "b", "A" }.CountBy(c => c, StringComparer.OrdinalIgnoreCase);
66+
var result = MoreEnumerable.CountBy(["a", "B", "c", "A", "b", "A"], c => c, StringComparer.OrdinalIgnoreCase);
6767

6868
result.AssertSequenceEqual(
6969
KeyValuePair.Create("a", 3),
@@ -76,7 +76,7 @@ public void CountByHasKeysOrderedLikeGroupBy()
7676
{
7777
var randomSequence = MoreEnumerable.Random(0, 100).Take(100).ToArray();
7878

79-
var countByKeys = randomSequence.CountBy(x => x).Select(x => x.Key);
79+
var countByKeys = MoreEnumerable.CountBy(randomSequence, x => x).Select(x => x.Key);
8080
var groupByKeys = randomSequence.GroupBy(x => x).Select(x => x.Key);
8181

8282
countByKeys.AssertSequenceEqual(groupByKeys);
@@ -85,7 +85,7 @@ public void CountByHasKeysOrderedLikeGroupBy()
8585
[Test]
8686
public void CountByIsLazy()
8787
{
88-
_ = new BreakingSequence<string>().CountBy(BreakingFunc.Of<string, int>());
88+
_ = MoreEnumerable.CountBy(new BreakingSequence<string>(), BreakingFunc.Of<string, int>());
8989
}
9090

9191
[Test]
@@ -95,7 +95,7 @@ public void CountByWithSomeNullKeys()
9595
{
9696
"foo", null, "bar", "baz", null, null, "baz", "bar", null, "foo"
9797
};
98-
var result = ss.CountBy(s => s);
98+
var result = MoreEnumerable.CountBy(ss, s => s);
9999

100100
result.AssertSequenceEqual(
101101
KeyValuePair.Create((string?)"foo", 2),
@@ -107,7 +107,8 @@ public void CountByWithSomeNullKeys()
107107
[Test]
108108
public void CountByWithSomeNullKeysAndEqualityComparer()
109109
{
110-
var result = new[] { "a", "B", null, "c", "A", null, "b", "A" }.CountBy(c => c, StringComparer.OrdinalIgnoreCase);
110+
string?[] source = ["a", "B", null, "c", "A", null, "b", "A"];
111+
var result = MoreEnumerable.CountBy(source, c => c, StringComparer.OrdinalIgnoreCase);
111112

112113
result.AssertSequenceEqual(
113114
KeyValuePair.Create((string?)"a", 3),

MoreLinq/CompatibilitySuppressions.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@
3131
<Left>lib/net6.0/MoreLinq.dll</Left>
3232
<Right>lib/net8.0/MoreLinq.dll</Right>
3333
</Suppression>
34+
<Suppression>
35+
<DiagnosticId>CP0002</DiagnosticId>
36+
<Target>M:MoreLinq.Extensions.CountByExtension.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1})</Target>
37+
<Left>lib/net8.0/MoreLinq.dll</Left>
38+
<Right>lib/net9.0/MoreLinq.dll</Right>
39+
</Suppression>
40+
<Suppression>
41+
<DiagnosticId>CP0002</DiagnosticId>
42+
<Target>M:MoreLinq.Extensions.CountByExtension.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})</Target>
43+
<Left>lib/net8.0/MoreLinq.dll</Left>
44+
<Right>lib/net9.0/MoreLinq.dll</Right>
45+
</Suppression>
46+
<Suppression>
47+
<DiagnosticId>CP0002</DiagnosticId>
48+
<Target>M:MoreLinq.MoreEnumerable.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1})</Target>
49+
<Left>lib/net8.0/MoreLinq.dll</Left>
50+
<Right>lib/net9.0/MoreLinq.dll</Right>
51+
</Suppression>
52+
<Suppression>
53+
<DiagnosticId>CP0002</DiagnosticId>
54+
<Target>M:MoreLinq.MoreEnumerable.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})</Target>
55+
<Left>lib/net8.0/MoreLinq.dll</Left>
56+
<Right>lib/net9.0/MoreLinq.dll</Right>
57+
</Suppression>
3458
<Suppression>
3559
<DiagnosticId>CP0002</DiagnosticId>
3660
<Target>M:MoreLinq.MoreEnumerable.SkipLast``1(System.Collections.Generic.IEnumerable{``0},System.Int32)</Target>

MoreLinq/CountBy.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ static partial class MoreEnumerable
3232
/// <param name="keySelector">Function that transforms each item of source sequence into a key to be compared against the others.</param>
3333
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>
3434

35-
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
35+
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
36+
#if !NET9_0_OR_GREATER
37+
this
38+
#endif
39+
IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
3640
{
37-
return source.CountBy(keySelector, null);
41+
return CountBy(source, keySelector, null);
3842
}
3943

4044
/// <summary>
@@ -50,7 +54,11 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
5054
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
5155
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>
5256

53-
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
57+
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
58+
#if !NET9_0_OR_GREATER
59+
this
60+
#endif
61+
IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
5462
{
5563
if (source == null) throw new ArgumentNullException(nameof(source));
5664
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));

MoreLinq/Experimental/Await.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static IEnumerable<TResult> Impl(
461461
var completed = false;
462462
var cancellationTokenSource = new CancellationTokenSource();
463463

464-
var enumerator = source.Index().GetEnumerator();
464+
var enumerator = MoreEnumerable.Index(source).GetEnumerator();
465465
IDisposable disposable = enumerator; // disables AccessToDisposedClosure warnings
466466

467467
try

MoreLinq/Extensions.g.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,11 @@ public static partial class CountByExtension
11731173
/// <param name="keySelector">Function that transforms each item of source sequence into a key to be compared against the others.</param>
11741174
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>
11751175

1176-
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
1176+
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
1177+
#if !NET9_0_OR_GREATER
1178+
this
1179+
#endif
1180+
IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
11771181
=> MoreEnumerable.CountBy(source, keySelector);
11781182

11791183
/// <summary>
@@ -1189,7 +1193,11 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
11891193
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
11901194
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>
11911195

1192-
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
1196+
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
1197+
#if !NET9_0_OR_GREATER
1198+
this
1199+
#endif
1200+
IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
11931201
=> MoreEnumerable.CountBy(source, keySelector, comparer);
11941202

11951203
}

MoreLinq/MoreLinq.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<NeutralLanguage>en-US</NeutralLanguage>
122122
<VersionPrefix>4.3.1</VersionPrefix>
123123
<Authors>MoreLINQ Developers.</Authors>
124-
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
124+
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0</TargetFrameworks>
125125
<DebugType>portable</DebugType>
126126
<GenerateDocumentationFile>true</GenerateDocumentationFile>
127127
<AssemblyName>MoreLinq</AssemblyName>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#nullable enable

0 commit comments

Comments
 (0)