dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'comparer')
Error message
Value cannot be null. (Parameter 'comparer')
What it means
AsyncObservable.Contains<TSource>(source, element, comparer) throws ArgumentNullException naming 'comparer' when the IEqualityComparer<TSource> is null. If you don't need custom equality, call the two-argument Contains overload which uses the default equality comparison.
Solutions
- Pass a non-null IEqualityComparer<TSource> (e.g. EqualityComparer<T>.Default) or drop the comparer argument to use the default overload
- Ensure DI/config resolution of the comparer actually returns an instance
- Fall back to EqualityComparer<T>.Default when the resolved comparer is null
Example fix
// before var has = source.Contains(target, comparer); // comparer is null // after var has = source.Contains(target, comparer ?? EqualityComparer<string>.Default);
Defensive patterns
Strategy: validation
Validate before calling
var safeComparer = comparer ?? EqualityComparer<TSource>.Default;
Type guard
public static bool HasComparer<T>(IEqualityComparer<T>? c) => c is not null;
Try / catch
try { var has = source.Contains(element, comparer); }
catch (ArgumentNullException ex) when (ex.ParamName == "comparer") { /* fall back to EqualityComparer<T>.Default or the 2-arg overload */ } Prevention
- Use the two-argument Contains when default equality suffices
- Default unresolved comparers to EqualityComparer<T>.Default
- Validate DI/config-provided comparers resolve to instances
When it happens
Trigger: Passing an explicitly null comparer: source.Contains(element, null); commonly a comparer resolved from DI/config or a static field that was null.
Common situations: Config-driven comparison strategies where the configured comparer type failed to instantiate; refactors where a default comparer field was removed or renamed.
Related errors
- Value cannot be null. (Parameter 'source')
- comparer
- ArgumentNullException(nameof(comparer))
- Value cannot be null. (Parameter 'comparer')
- comparer
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/7ba8ead9206832f4.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Contains.cs:27
public partial class AsyncObservable
{
public static IAsyncObservable<bool> Contains<TSource>(this IAsyncObservable<TSource> source, TSource element)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return CreateAsyncObservable<bool>.From(
source,
element,
static (source, element, observer) => source.SubscribeSafeAsync(AsyncObserver.Contains(observer, element)));
}
public static IAsyncObservable<bool> Contains<TSource>(this IAsyncObservable<TSource> source, TSource element, IEqualityComparer<TSource> comparer)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (comparer == null)
throw new ArgumentNullException(nameof(comparer));
return CreateAsyncObservable<bool>.From(
source,
(element, comparer),
static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Contains(observer, state.element, state.comparer)));
}
}
public partial class AsyncObserver
{
public static IAsyncObserver<TSource> Contains<TSource>(IAsyncObserver<bool> observer, TSource element)
{
if (observer == null)
throw new ArgumentNullException(nameof(observer));
return Contains(observer, element, EqualityComparer<TSource>.Default);
}
View on GitHub (pinned to 94b5d5ab91)