C# Error CS0845 – An expression tree lambda may not contain a coalescing operator with a null or default literal left-hand side

C# Error

CS0845 – An expression tree lambda may not contain a coalescing operator with a null or default literal left-hand side

Reason for the Error & Solution

An expression tree lambda may not contain a coalescing operator with a null literal left-hand side.

Because null by itself does not have a type, the null coalescing operator cannot operate on it.

To correct this error

  1. Cast the null literal to an object.

Example

The following code generates CS0845:

// cs0845.cs  
using System;  
using System.Linq;  
using System.Linq.Expressions;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Expression<Func<object>> e = () => null ?? null; // CS0845  
            // Try the following line instead.  
            // Expression<Func<object>> e = () => (object)null ?? null;  
        }  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...