{"record":{"id":"04d9df6c424156c0","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-do","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs","lineNumber":21,"sourceCode":"// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Lazily invokes an action for each value in the sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element.</param>\n        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>\n        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNext == null)\n                throw new ArgumentNullException(nameof(onNext));\n\n            return DoCore(source, onNext, _ => { }, () => { });\n        }\n\n        /// <summary>\n        /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element.</param>\n        /// <param name=\"onCompleted\">Action to invoke on successful termination of the sequence.</param>\n        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>\n        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs#L3-L39","documentation":"Do(source, onNext) taps each element of the sequence with an action, and it validates the source sequence up front: a null IEnumerable<TSource> throws ArgumentNullException with parameter name 'source'. The check occurs at composition time, so you get the exception at the line that built the pipeline rather than during enumeration. This is part of the operator's fail-fast argument contract.","triggerScenarios":"Calling EnumerableEx.Do(null, onNext) or a chain like stream.Do(...) where stream is null — typically null return values from services, uninitialized fields, or nullable model properties.","commonSituations":"Logging/side-effect pipelines bolted onto a source that turned out to be null, repository methods returning null instead of empty, deserialized objects with null sequence properties, tests passing null.","solutions":["Coalesce with an empty sequence: (source ?? Enumerable.Empty<T>()).Do(onNext).","Fix the producer to return Enumerable.Empty<T>() instead of null.","Validate the source at your API boundary and throw a descriptive exception.","Ensure the variable feeding the chain is initialized before pipeline construction."],"exampleFix":"// before\nvar seq = GetItems(); // may return null\nvar logged = seq.Do(x => Console.WriteLine(x));\n// after\nvar logged = (GetItems() ?? Enumerable.Empty<Item>()).Do(x => Console.WriteLine(x));","handlingStrategy":"validation","validationCode":"if (source is null) source = Enumerable.Empty<TSource>();\nvar result = source.Do(onNext);","typeGuard":"static bool IsSequence<TSource>(IEnumerable<TSource>? s) => s is not null;","tryCatchPattern":"try\n{\n    var result = source.Do(onNext);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    result = Enumerable.Empty<TSource>();\n}","preventionTips":["Never let sequence-producing methods return null; return Enumerable.Empty<T>()","Coalesce before adding side-effect operators like Do","Use nullable reference types to catch null sources statically","Validate external inputs before wrapping them in LINQ pipelines"],"tags":["argumentnull","linq","ix-net","null-check"],"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"}