Skip to main content

How to Resolve Error "CS0845: An expression tree lambda may not contain a coalescing operator with a null or default literal left-hand side" in C#

The Compiler Error CS0845 is a specific restriction found when creating Expression Trees. The message reads: "An expression tree lambda may not contain a coalescing operator with a null or default literal left-hand side".

In standard C# code, writing var x = null ?? "default"; is valid (though pointless), and the compiler usually optimizes it away. However, Expression Trees represent code structure as data. The C# compiler refuses to create an ExpressionType.Coalesce node if the left side is a known null literal or default literal, because the operation is logically redundant and cannot be represented efficiently in the tree structure the compiler generates.

This guide explains how to remove this redundancy to fix the error.

Understanding the Restriction

The Null Coalescing Operator (??) is used to return the left-hand operand if it isn't null; otherwise, it returns the right-hand operand.

  • Logic: Left ?? Right
  • If Left is Literal Null: null ?? Right always equals Right.

When building an Expression Tree, the compiler expects the left side to be an expression that could be null (like a property, a parameter, or a variable). If you explicitly put null there, the compiler treats it as a syntax error because it refuses to generate a "Coalesce" node for an operation that does nothing.

Scenario 1: Using Literal Null (null ??)

This is the most direct cause. You are defining an Expression<Func<...>> and explicitly writing null on the left side of the ?? operator.

Example of error

using System;
using System.Linq.Expressions;

public class Program
{
static void Main()
{
// ⛔️ Error CS0845: An expression tree lambda may not contain a coalescing
// operator with a null or default literal left-hand side.
// The compiler sees "null ?? x" and knows this is redundant.
Expression<Func<string, string>> expr = x => null ?? "Default";
}
}

Solution: Simplify the Expression

Since null ?? X is always X, simply remove the coalescing operator and use the right-hand value directly.

using System;
using System.Linq.Expressions;

public class Program
{
static void Main()
{
// ✅ Correct: Just return the value.
// The logic is identical, but the invalid tree node is removed.
Expression<Func<string, string>> expr = x => "Default";

Console.WriteLine(expr.Compile().Invoke("Input")); // Output: "Default"
}
}
note

If you are generating this code dynamically and cannot "see" that it is null, ensure your generator checks if the left expression is a ConstantExpression with a value of null before creating a Coalesce node.

Scenario 2: Using default(T) Literal

The keyword default(T) for reference types (like string or object) resolves to null. Using this on the left side of ?? triggers the same error because the compiler treats default(string) as a null literal in this context.

Example of error

using System;
using System.Linq.Expressions;

public class Data
{
public void BuildQuery()
{
// ⛔️ Error CS0845: 'default(string)' is effectively 'null'.
Expression<Func<string, string>> expr = s => default(string) ?? "Fallback";
}
}

Solution: Use Variables or Remove Logic

If you intended to check a specific variable, use that variable. If you truly meant default(string), simply remove the check.

Option A: Remove Redundancy

// ✅ Correct: 'default(string)' is null, so the result is always "Fallback".
Expression<Func<string, string>> expr = s => "Fallback";

Option B: Use a Variable (If dynamic)

If the value comes from a variable (even if that variable currently holds null), the compiler allows it because the variable could change.

public void BuildQuery()
{
string dynamicValue = null;

// ✅ Correct: A variable (Closure) is allowed on the left side.
// The expression tree can represent "Check the value of this variable".
Expression<Func<string, string>> expr = s => dynamicValue ?? "Fallback";
}

Conclusion

CS0845 is the compiler's way of rejecting useless logic in Expression Trees.

  1. Analyze the Expression: Look at the ?? operator.
  2. Check the Left Side: Is it null or default(Type)?
  3. The Fix: Remove the ?? operator entirely and keep only the right-hand side, as the logic will always evaluate to that anyway.