Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created December 13, 2024 01:54
Show Gist options
  • Save ufcpp/0b4a77b8e129eb79a47c237719b602cf to your computer and use it in GitHub Desktop.
Save ufcpp/0b4a77b8e129eb79a47c237719b602cf to your computer and use it in GitHub Desktop.
CS9236 with the Sum extension method overloads.
using System.Runtime.CompilerServices;
using static Enumerable;
int[][] x = [[]];
var y = x.Sum(x => x.Sum(x => 1));
#if false
// Same as System.Linq.Enumerable, this causes CS9236.
static class Enumerable
{
public static float Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector) => throw new NotImplementedException();
public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector) => throw new NotImplementedException();
public static long Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector) => throw new NotImplementedException();
public static decimal? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector) => throw new NotImplementedException();
public static double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector) => throw new NotImplementedException();
public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector) => throw new NotImplementedException();
public static long? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector) => throw new NotImplementedException();
public static float? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector) => throw new NotImplementedException();
public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector) => throw new NotImplementedException();
public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector) => throw new NotImplementedException();
}
#else
// Make the INumber overload public and decrease the priority of int's and other overloads.
static class Enumerable
{
public static TResult Sum<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
where TResult : struct, System.Numerics.INumber<TResult> => throw new NotImplementedException();
public static TResult? Sum<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult?> selector)
where TResult : struct, System.Numerics.INumber<TResult> => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static float Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static long Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static decimal? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static long? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static float? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector) => throw new NotImplementedException();
[OverloadResolutionPriority(-1)] public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector) => throw new NotImplementedException();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment