{"record":{"id":"025aedc9b0bba164","repo":"dotnet/reactive","slug":"nameof-predicate-observable-blocking","errorCode":null,"errorMessage":"nameof(predicate)","messagePattern":"nameof\\(predicate\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs","lineNumber":540,"sourceCode":"        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source observable sequence.</param>\n        /// <param name=\"predicate\">A predicate function to evaluate for elements in the source sequence.</param>\n        /// <returns>The single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"predicate\"/> is null.</exception>\n        /// <exception cref=\"InvalidOperationException\">The sequence contains more than one element that satisfies the condition in the predicate.</exception>\n        /// <seealso cref=\"Observable.SingleOrDefaultAsync{TSource}(IObservable{TSource}, Func{TSource, bool})\"/>\n        [Obsolete(Constants_Linq.UseAsync)]\n        [return: MaybeNull]\n        public static TSource SingleOrDefault<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (predicate == null)\n            {\n                throw new ArgumentNullException(nameof(predicate));\n            }\n\n            return s_impl.SingleOrDefault(source, predicate);\n        }\n\n        #endregion\n\n        #region + Wait +\n\n        /// <summary>\n        /// Waits for the observable sequence to complete and returns the last element of the sequence.\n        /// If the sequence terminates with an OnError notification, the exception is thrown.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source observable sequence.</param>\n        /// <returns>The last element in the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"InvalidOperationException\">The source sequence is empty.</exception>","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs#L522-L558","documentation":"The message is \"Value cannot be null. (Parameter 'predicate')\" thrown at Observable.Blocking.cs:540: SingleOrDefault with a filter predicate requires a non-null Func<TSource,bool>, and it is validated immediately after the source check. The predicate is applied synchronously to each element while blocking for the single result, so a null predicate has no defined behavior and the operator rejects it up front.","triggerScenarios":"Calling Observable.SingleOrDefault<TSource>(source, null) with a null predicate — commonly because the predicate was built conditionally, came from an uninitialized delegate field, or was received as a null method-group/lambda result from another API.","commonSituations":"Conditional filtering logic where the predicate variable is only assigned on one branch; a strategy/predicate injected via configuration or DI that resolved to null; copying an async LINQ pattern where the predicate is optional and passing null 'to skip' it.","solutions":["Ensure a concrete predicate is supplied: obs.SingleOrDefault(x => x.Id == id);","If filtering is optional, branch: apply the predicate overload only when a predicate exists, otherwise call the parameterless SingleOrDefault overload (line 513).","Null-check the delegate before calling, and fix whatever produced the null predicate (config binding, factory, DI registration)."],"exampleFix":"// before\nFunc<Order, bool> filter = GetFilter(); // may return null\nvar order = orders.SingleOrDefault(filter); // ArgumentNullException('predicate')\n// after\nvar order = filter != null\n    ? orders.SingleOrDefault(filter)\n    : orders.SingleOrDefault();","handlingStrategy":"validation","validationCode":"if (predicate is null)\n{\n    value = source.SingleOrDefault(); // optional-filter path\n}\nelse\n{\n    value = source.SingleOrDefault(predicate);\n}","typeGuard":"bool HasFilter<T>(Func<T, bool>? predicate) => predicate is not null;","tryCatchPattern":"try\n{\n    var value = source.SingleOrDefault(pred);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"predicate\")\n{\n    // predicate was null; fall back to unfiltered single element\n    value = source.SingleOrDefault();\n}","preventionTips":["Treat 'no filter' as a separate overload call, not as a null predicate.","Guard conditionally-built predicates with a default (x => true) instead of null.","Check DI/config bindings that supply predicate delegates actually resolve."],"tags":["rx","argument-null","predicate","linq"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}