{"record":{"id":"a3153efbabbf3688","repo":"louthy/language-ext","slug":"argumentnullexception","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Guard.cs","lineNumber":18,"sourceCode":"﻿using System;\nusing static LanguageExt.Prelude;\n\nnamespace LanguageExt;\n\n/// <summary>\n/// Used by various error-producing monads to have a contextual `where`\n/// </summary>\n/// <remarks>\n/// See `Prelude.guard(...)`\n/// </remarks>\npublic readonly struct Guard<E, A>\n{\n    public readonly bool Flag;\n    readonly Func<E> onFalse;\n\n    internal Guard(bool flag, Func<E> onFalse) =>\n        (Flag, this.onFalse) = (flag, onFalse ?? throw new ArgumentNullException(nameof(onFalse)));\n\n    internal Guard(bool flag, E onFalse)\n    {\n        if (isnull(onFalse)) throw new ArgumentNullException(nameof(onFalse));\n        (Flag, this.onFalse) = (flag, () => onFalse);\n    }\n\n    public Guard<E, B> Cast<B>() =>\n        new (Flag, OnFalse);\n        \n    public Func<E> OnFalse =>\n        onFalse ?? throw new InvalidOperationException(\n            \"Guard isn't initialised. It was probably created via new Guard() or default(Guard), and so it has no OnFalse handler\");\n\n    public Guard<E, C> SelectMany<C>(Func<E, Guard<E, Unit>> bind, Func<Unit, Unit, C> project) =>\n        Flag ? bind(default!).Cast<C>() : Cast<C>();\n\n    public Guard<E, B> Select<B>(Func<B, B> _) =>","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Guard.cs#L1-L36","documentation":"Guard's internal constructor taking Func<E> onFalse throws ArgumentNullException when the onFalse delegate is null. A Guard must always know how to produce its error value E for the false branch, so a null factory is rejected at construction.","triggerScenarios":"Calling new Guard<E,A>(flag, (Func<E>)null) or passing a null lambda through the internal API (e.g. from generated/language-ext combinators that propagate a null onFalse).","commonSituations":"Refactoring where a lazily-evaluated error factory variable became null, or binding results of optional error-providing functions directly into Guard.","solutions":["Provide a non-null error factory when constructing Guard.","Use the E-based constructor with a default error value instead of a null Func.","Coalesce: onFalse ?? (() => defaultError) before constructing."],"exampleFix":"// before\nvar g = new Guard<E, A>(flag, maybeFactory); // maybeFactory may be null\n// after\nvar g = new Guard<E, A>(flag, maybeFactory ?? (() => defaultError));","handlingStrategy":"validation","validationCode":"onFalse ?? throw new ArgumentNullException(nameof(onFalse));","typeGuard":"static bool ValidFactory<E>(Func<E> f) => f is not null;","tryCatchPattern":"try { var g = new Guard<E,A>(flag, f); } catch (ArgumentNullException) { /* null factory */ }","preventionTips":["Always supply a concrete error factory","Coalesce null factories with defaults","Enable nullable reference type checks so null lambdas are caught at compile time"],"tags":["guard","null","argument","csharp"],"backgroundTag":"null-argument","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}