Compiler Error CS0845

表达式树 lambda 不能在合并运算符左侧包含 null 文本。

由于 null 本身不具有类型,因此 null 合并运算符无法对其进行运算。

更正此错误

  1. 将 null 文本强制转换为对象。

下面的代码将生成 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;
        }
    }
}