{"record":{"id":"2039ab2b78e4c5a3","repo":"dotnet/reactive","slug":"source-last","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Last.cs","lineNumber":18,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source) => Last(source);\n        public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => Last(source, predicate);\n        public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => Last(source, predicate);\n\n        public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(\n                source,\n                (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer)));\n        }\n\n        public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer, predicate)));\n        }","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Last.cs#L1-L36","documentation":"The Last operator throws ArgumentNullException because the source IAsyncObservable<TSource> was null. Last emits the final element (optionally matching a predicate) of a sequence; a null source has no elements to observe, so the library fails fast at operator creation. Called via LastAsync/Last extension methods.","triggerScenarios":"Calling source.Last() or source.LastAsync(predicate) where source is null — e.g. a repository method returned null instead of an empty observable, or a nullable field was never assigned.","commonSituations":"Chaining off a factory method that returns null on failure, conditional pipeline assembly where an upstream step was skipped, or dictionary/config lookups missing a key.","solutions":["Pass a non-null IAsyncObservable<TSource> to Last/LastAsync.","If the source may be absent, use AsyncObservable.Empty<TSource>() so Last surfaces an empty-sequence error through normal semantics instead.","Trace and fix the upstream producer that returned null instead of a valid observable."],"exampleFix":"// before\nvar last = await maybeSource!.Last().FirstOrDefaultAsync();\n// after\nvar src = maybeSource ?? AsyncObservable.Empty<TSource>();\nvar last = await src.Last().FirstOrDefaultAsync();","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentException(\"source observable must be non-null before calling Last\", nameof(source));","typeGuard":"bool SourceReady<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var last = await source.Last(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fix the null producer or fall back to an empty observable */ }","preventionTips":["Have all observable-producing methods return Empty rather than null on failure.","Enable C# nullable reference types to catch null sources at compile time.","Null-check results of config/dictionary-based source lookups before chaining operators."],"tags":["argument-null","reactive-operators","last"],"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"}