{"record":{"id":"70cc1c0091996f95","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-cast","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":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Cast.cs","lineNumber":12,"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\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TResult> Cast<TSource, TResult>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, TResult>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Cast<TSource, TResult>(observer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Cast<TSource, TResult>(IAsyncObserver<TResult> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Select<TSource, TResult>(observer, x => (TResult)(object)x);\n        }\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Cast.cs#L1-L29","documentation":"Cast<TSource,TResult> is an extension on IAsyncObservable<TSource> and throws ArgumentNullException when the source is null. Like all AsyncRx operators it validates inputs up front rather than failing inside the subscription pipeline.","triggerScenarios":"Invoking source.Cast<TSource,TResult>() on a null IAsyncObservable<TSource>, or Cast(null) via static call syntax.","commonSituations":"Chaining off a factory method that returned null; LINQ-style pipelines where an earlier operator produced null; conditional source assignment left unset.","solutions":["Ensure the source observable is non-null before calling Cast","Replace a null source with AsyncObservable.Empty<TSource>() when 'no source' is a valid state","Null-check the method returning the observable and handle the null branch explicitly"],"exampleFix":"// before\nIAsyncObservable<object> src = maybeNull;\nvar ints = src.Cast<object, int>();\n// after\nif (src == null) src = AsyncObservable.Empty<object>(Scheduler.Default);\nvar ints = src.Cast<object, int>();","handlingStrategy":"validation","validationCode":"if (source == null) throw new InvalidOperationException(\"Cannot Cast a null source observable\");","typeGuard":"bool IsObservable<T>(IAsyncObservable<T> s) => s is not null;","tryCatchPattern":"try { var result = src.Cast<object, int>(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fall back to Empty source */ }","preventionTips":["Never let factories return null observables; return Empty instead","Use ?? Empty(scheduler) when composing optional streams","Check extension-method receivers for null in pipelines"],"tags":["argument-null","reactive-extensions","cast"],"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"}