{"record":{"id":"96509b49b79f873e","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-tohashset","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToHashSet.cs","lineNumber":38,"sourceCode":"        {\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\n        public static IAsyncObserver<TSource> ToHashSet<TSource>(IAsyncObserver<HashSet<TSource>> observer, IEqualityComparer<TSource> comparer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return Aggregate<TSource, HashSet<TSource>>(observer, new HashSet<TSource>(comparer), (set, x) => { set.Add(x); return set; });\n        }\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToHashSet.cs#L20-L54","documentation":"AsyncObserver.ToHashSet(observer) wraps a downstream observer to accumulate elements into a HashSet. The downstream observer must be non-null; passing null throws ArgumentNullException(Parameter 'observer') synchronously. This guard protects the operator's internal ObserverBase wiring from a null downstream sink.","triggerScenarios":"Calling AsyncObserver.ToHashSet(null), typically when building custom operator plumbing where the observer argument came from an uninitialized variable or an earlier Create call that passed null.","commonSituations":"Writing custom AsyncObservable operators with Create<TSource,TResult> and accidentally forwarding a null observer; unit-test scaffolding that forgot to construct a test observer.","solutions":["Pass a valid IAsyncObserver<HashSet<TSource>> instance to AsyncObserver.ToHashSet.","Validate the observer argument in your own operator before forwarding it.","Ensure the observer comes from the Create callback's observer parameter, not a separately managed null field."],"exampleFix":"// before\nvar obs = AsyncObserver.ToHashSet<int>(observer); // observer null\n// after\nif (observer == null) throw new InvalidOperationException(\"observer not wired\");\nvar obs = AsyncObserver.ToHashSet<int>(observer);","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer));","typeGuard":"bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObserver.ToHashSet(observer); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* fix observer wiring */ }","preventionTips":["Always forward the observer parameter from the Create callback","Avoid storing observers in nullable fields","Write custom-operator tests that subscribe immediately to catch unwired observers"],"tags":["null-argument","asyncrx","observer"],"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"}