{"record":{"id":"e93defddb6a514f1","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-e93def","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/TakeLast.cs","lineNumber":22,"sourceCode":"\nusing System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n#if !(REFERENCE_ASSEMBLY && (NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER ))\n        /// <summary>\n        /// Returns a specified number of contiguous elements from the end of the sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"count\">The number of elements to take from the end of the sequence.</param>\n        /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>\n        public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<TSource> source, int count)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count < 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n\n            return TakeLastCore(source, count);\n        }\n\n        private static IEnumerable<TSource> TakeLastCore<TSource>(IEnumerable<TSource> source, int count)\n        {\n            if (count == 0)\n            {\n                yield break;\n            }\n\n            var q = new Queue<TSource>(count);\n\n            foreach (var item in source)\n            {\n                if (q.Count >= count)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/TakeLast.cs#L4-L40","documentation":"TakeLast returns the final `count` elements of a sequence. The operator validates `source` eagerly when the method is called and throws ArgumentNullException naming `source` if it is null. This is fail-fast design: the error surfaces at the operator call site, not during MoveNext.","triggerScenarios":"Calling `TakeLast(null, 5)` or `someNullableSeq.TakeLast(5)` where the source IEnumerable<TSource> is null.","commonSituations":"Feeding results of a lookup method that returns null on miss into TakeLast, unguarded nullable fields, or LINQ chains built from external data (files, APIs, DB) where a missing entity maps to null.","solutions":["Coalesce the source: `(source ?? Enumerable.Empty<T>()).TakeLast(n)`.","Fix the producer so it never returns null for a sequence.","Return early when the source is null instead of building the query.","Log/inspect the upstream path if null is unexpected — the bug is usually upstream, not in TakeLast."],"exampleFix":"// before\nvar last3 = possiblyNull.TakeLast(3);\n// after\nvar last3 = (possiblyNull ?? Enumerable.Empty<int>()).TakeLast(3);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\n// or: var safe = source ?? Enumerable.Empty<T>();","typeGuard":"static bool HasSource<T>(IEnumerable<T> s) => s is not null;","tryCatchPattern":"try { var last = seq.TakeLast(n); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fall back to empty */ }","preventionTips":["Treat null as 'empty' by normalizing producers to Enumerable.Empty.","Use nullable annotations on pipeline variables.","Keep null-checks at data boundaries, not sprinkled in LINQ chains.","Add contract tests asserting producers never yield null sequences."],"tags":["linq","null-argument","ix-interactive"],"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"}