Tag: null coalescing operator

表达式树lambda可能不包含空传播运算符

问题 :线路price = co?.price ?? 0, price = co?.price ?? 0,在下面的代码中给出了我上面的错误。 但如果我删除? 来自co.? 它工作正常。 我试图按照他们使用的MSDN示例进行操作? 在线select new { person.FirstName, PetName = subpet?.Name ?? String.Empty }; select new { person.FirstName, PetName = subpet?.Name ?? String.Empty }; 那么,似乎我需要了解何时使用? 与?? 什么时候不去。 错误 : 表达式树lambda可能不包含空传播运算符 public class CustomerOrdersModelView { public string CustomerID { get; set; } public int FY […]

奇怪的运算符优先级与?? (null合并运算符)

最近我有一个奇怪的错误,我用字符串连接int? 然后在那之后添加另一个字符串。 我的代码基本上相当于: int? x=10; string s = “foo” + x ?? 0 + “bar”; 令人惊讶的是,这将运行和编译没有警告或不兼容的类型错误,这将是: int? x=10; string s = “foo” + x ?? “0” + “bar”; 然后这会导致意外的类型不兼容错误: int? x=10; string s = “foo” + x ?? 0 + 12; 这个更简单的例子也是如此: int? x=10; string s = “foo” + x ?? 0; 有人能解释一下这对我有用吗?