{"record":{"id":"67433c66a3188a24","repo":"dotnet/reactive","slug":"argumentnullexception-default-message-value-cannot-be-null-67433c","errorCode":null,"errorMessage":"ArgumentNullException (default message: Value cannot be null. (Parameter 'comparer'))","messagePattern":"ArgumentNullException \\(default message: Value cannot be null\\. \\(Parameter 'comparer'\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs","lineNumber":113,"sourceCode":"        /// <summary>\n        /// Returns an observable sequence that contains only distinct elements according to the comparer.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">An observable sequence to retain distinct elements for.</param>\n        /// <param name=\"comparer\">Equality comparer for source elements.</param>\n        /// <returns>An observable sequence only containing the distinct elements from the source sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"comparer\"/> is null.</exception>\n        /// <remarks>Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.</remarks>\n        public static IObservable<TSource> Distinct<TSource>(this IObservable<TSource> source, IEqualityComparer<TSource> comparer)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (comparer == null)\n            {\n                throw new ArgumentNullException(nameof(comparer));\n            }\n\n            return s_impl.Distinct(source, comparer);\n        }\n\n        /// <summary>\n        /// Returns an observable sequence that contains only distinct elements according to the keySelector.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TKey\">The type of the discriminator key computed for each element in the source sequence.</typeparam>\n        /// <param name=\"source\">An observable sequence to retain distinct elements for.</param>\n        /// <param name=\"keySelector\">A function to compute the comparison key for each element.</param>\n        /// <returns>An observable sequence only containing the distinct elements, based on a computed key value, from the source sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"keySelector\"/> is null.</exception>\n        /// <remarks>Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.</remarks>\n        public static IObservable<TSource> Distinct<TSource, TKey>(this IObservable<TSource> source, Func<TSource, TKey> keySelector)\n        {\n            if (source == null)","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs#L95-L131","documentation":"The Distinct(source, comparer) overload requires both arguments non-null; this throw site is the comparer == null check that runs after source validation. The comparer is needed to build the internal deduplication set, so the library rejects null eagerly.","triggerScenarios":"Calling Distinct<TSource>(source, null) — a valid source but a null IEqualityComparer<TSource>.","commonSituations":"A comparer resolved from configuration/DI was not registered, or a custom comparer variable was never assigned before building the query.","solutions":["Pass a valid comparer instance, e.g., EqualityComparer<TSource>.Default for default semantics","Fix the factory/DI registration that was supposed to supply the comparer","If default equality is acceptable, use the comparer-less Distinct() overload instead","Add a null check on the comparer at construction time"],"exampleFix":"// before\nIEqualityComparer<string> cmp = _comparerProvider?.Get(); // may be null\nvar result = source.Distinct(cmp);\n// after\nvar result = source.Distinct(cmp ?? EqualityComparer<string>.Default);","handlingStrategy":"validation","validationCode":"if (comparer is null) throw new ArgumentNullException(nameof(comparer));\n// or: comparer ??= EqualityComparer<TSource>.Default;","typeGuard":"bool HasComparer<TSource>(IEqualityComparer<TSource>? comparer) => comparer is not null;","tryCatchPattern":"try { var result = source.Distinct(comparer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"comparer\")\n{\n    result = source.Distinct(); // fall back to default equality\n}","preventionTips":["Pass EqualityComparer<T>.Default instead of null when default semantics are fine","Check that comparer-providing services are registered in DI before use","Assign comparer variables at declaration, never leave them conditionally unset","Prefer the comparer-less Distinct() overload when a custom comparer is optional"],"tags":["csharp","rx","argument-validation","null","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-23T08:17:48.524Z"}