{"record":{"id":"1d174722d12db036","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-comparer-tohashset","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'comparer')","messagePattern":"Value cannot be null\\. \\(Parameter 'comparer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToHashSet.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<HashSet<TSource>> ToHashSet<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, HashSet<TSource>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToHashSet(observer)));\n        }\n\n        public static IAsyncObservable<HashSet<TSource>> ToHashSet<TSource>(this IAsyncObservable<TSource> source, IEqualityComparer<TSource> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return CreateAsyncObservable<HashSet<TSource>>.From(\n                source,\n                comparer,\n                static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.ToHashSet(observer, comparer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ToHashSet<TSource>(IAsyncObserver<HashSet<TSource>> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return ToHashSet(observer, EqualityComparer<TSource>.Default);\n        }\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToHashSet.cs#L6-L42","documentation":"The comparer overload of AsyncObservable.ToHashSet requires a non-null IEqualityComparer<TSource> used to build the internal HashSet and to deduplicate incoming elements. Passing null throws ArgumentNullException(Parameter 'comparer') synchronously at the call site.","triggerScenarios":"Calling ToHashSet(source, comparer) with comparer = null, typically a comparer variable that failed to initialize or a ternary that fell through to null.","commonSituations":"Injecting a comparer via DI that was not registered; loading a comparer from configuration; passing null intending to mean 'use default comparer' instead of calling the parameterless overload.","solutions":["Call the parameterless ToHashSet(source) overload when default equality is desired.","Pass EqualityComparer<TSource>.Default explicitly instead of null.","Fix the comparer construction so it returns a real instance rather than null."],"exampleFix":"// before\nvar res = source.ToHashSet(comparer); // comparer is null\n// after\nvar res = comparer != null ? source.ToHashSet(comparer) : source.ToHashSet();","handlingStrategy":"validation","validationCode":"if (comparer == null) comparer = EqualityComparer<TSource>.Default;","typeGuard":"bool IsValidComparer<T>(IEqualityComparer<T> c) => c is not null;","tryCatchPattern":"try { var set = await source.ToHashSet(comparer); } catch (ArgumentNullException ex) when (ex.ParamName == \"comparer\") { /* retry with default comparer */ }","preventionTips":["Use the parameterless overload when default equality suffices","Never store comparer dependencies as nullable fields without defaults","Register custom comparators in DI with non-null defaults"],"tags":["null-argument","asyncrx","comparer"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}