dotnet/reactive · error · ArgumentNullException

Thrown when keySelector is null (ArgumentNullException…

Error message

Thrown when keySelector is null (ArgumentNullException, param name: keySelector)

What it means

MaxByWithTies(source, keySelector) throws ArgumentNullException with param name 'keySelector' when the key-selection delegate is null. The delegate is required to extract a comparison key from every element, so the operator refuses to run without it.

Solutions

  1. Ensure a non-null lambda or method group is passed as the key selector
  2. Guard the delegate before the call: if (keySelector == null) use a default key extractor
  3. Check where the delegate is produced (DI, config, factory) and fix the null origin

Example fix

// before
Func<Order, decimal> key = GetSelector() ; // may be null
var top = orders.MaxByWithTies(key);
// after
var key = GetSelector() ?? (o => o.Total);
var top = orders.MaxByWithTies(key);
Defensive patterns

Strategy: validation

Validate before calling

if (keySelector is null) throw new ArgumentException("Key selector must not be null.", nameof(keySelector));
var ties = source.MaxByWithTies(keySelector);

Type guard

static bool IsValidSelector<T,TKey>(Func<T,TKey> f) => f is not null;

Try / catch

try { result = source.MaxByWithTies(keySelector); }
catch (ArgumentNullException ex) when (ex.ParamName == "keySelector") { /* fall back to default selector */ }

Prevention

When it happens

Trigger: Passing a null Func<TSource,TKey>, e.g. Func<Person,int> key = null; people.MaxByWithTies(key); typically when the delegate is built conditionally or fetched from config/dependency injection.

Common situations: Conditionally constructed lambdas, delegates resolved from a DI container or settings object that returned null, refactoring that renamed a method leaving a null method-group reference.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/de0e96e087418055. Report an issue: GitHub.

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxByWithTies.cs:24

namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Returns the elements with the maximum key value by using the default comparer to compare key values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TKey">Key type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
        /// <returns>List with the elements that share the same maximum key value.</returns>
        public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));

            return MaxByWithTies(source, keySelector, Comparer<TKey>.Default);
        }

        /// <summary>
        /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TKey">Key type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
        /// <param name="comparer">Comparer used to determine the maximum key value.</param>
        /// <returns>List with the elements that share the same maximum key value.</returns>
        public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)

View on GitHub (pinned to 94b5d5ab91)