简体   繁体   English

`?.`有什么作用?

[英]What does `?.` do?

How can I express the following linq query without using 我如何不使用以下方式表达以下linq查询

ItemSupplierName = u?.SupplierName ItemSupplierName = u?.SupplierName

I keep getting a CS1525, CS1003 error message when trying to compile it on my build agent at the above line. 尝试在上一行的构建代理中编译CS1525,CS1003错误消息时,我不断收到该消息。

- CS1525 Invalid expression term '.'
- CS1003 Syntax error, ':' expected

Code below, it runs perfectly locally but fails on the build agent. 下面的代码可在本地完美运行,但无法在构建代理上运行。

ExpandedItems = from t in items
                               from u in t.Supplier.DefaultIfEmpty()
select new {
ItemName = t.Name
ItemSupplierName = u?.SupplierName
}

The ?. ?. syntax is a new feature in C#6 and is a short cut for checking that the variable isn't null before deferencing it. 语法是C#6中的新功能,是在递归之前检查变量是否为null的捷径。 The fact that you're getting that error on the build server shows that the build server is still running an older version of the compiler. 您在构建服务器上收到该错误的事实表明,构建服务器仍在运行较旧版本的编译器。

If you can, get the build agent machine upgraded to use the same version of C# and .NET that you're using to develop with. 如果可以,请升级构建代理机器,以使用与您用于开发的相同版本的C#和.NET。

If you can't get the build agent upgraded to use the latest version of C# (and .NET) then you'll have to go back to the old way of checking for null: 如果您无法升级构建代理以使用最新版本的C#(和.NET),则必须返回到检查null的旧方法:

ItemSupplierName = u != null ? u.SupplierName : null

This is the so called " null conditional operator ", which is available in C# 6 and later. 这就是所谓的“ 空条件运算符 ”,在C#6和更高版本中可用。 It makes sure that the given term evaluates to null if the part before the ? 如果在?之前的部分,则确保给定项的值为null ? is null . null

For example: 例如:

String x = objectVariable?.StringProperty;

assigns null to x in case objectVariable or StringProperty are null . 如果objectVariableStringPropertynullx分配null If both are not null, x will contain the value of StringProperty . 如果两者都不为null,则x将包含StringProperty的值。

The nice thing about it is that you can chain it: 它的好处是您可以将其链接:

String s = object1?.SomeList?[0]?.SubProperty?.Value;

This replaces structures like: 这将替换如下结构:

String s = null;
if (object1 != null && 
    object1.SomeList!= null && 
    object1.SomeList[0] != null &&
    object1.SomeList[0].SubProperty != null)
  s = object1.SomeProperty.SubProperty.Value;

Another way to use it is when raising events. 使用它的另一种方法是引发事件。 Up to C# 6 you wrote something like this: 在C#6之前,您编写了以下内容:

private event EventHandler<EventArgs> MyEvent;

if (MyEvent != null)
    MyEvent(this, EventArgs.Empty);

Now you can just write 现在你可以写

MyEvent?.Invoke(this, EventArgs.Empty);

Your code is C#6 code, it looks like you are trying to compile it with an older version. 您的代码是C#6代码,似乎您正在尝试使用旧版本进行编译。

You have to replace u? 你要替换u? with a ternary operator: 使用三元运算符:

ExpandedItems = from t in items
                from u in t.Supplier.DefaultIfEmpty()
                select new {
                    ItemName = t.Name
                    ItemSupplierName = (u == null ? null : u.SupplierName)
                }

Have a look here , search for "Null-conditional operators". 在这里看看,搜索“空条件运算符”。

As @ChrisF has mentioned above - You'll need to upgrade build agent to the latest version. 正如@ChrisF前面提到的-您需要将构建代理升级到最新版本。

Operator ?. 运算符?. is the new syntax of C# 6 and allows for user check if the object is null. 是C#6的新语法,允许用户检查对象是否为null。 If result of operator is null, it returns null itself instead of resolving the inner variable/attribute. 如果operator的结果为null,则它本身返回null而不是解析内部变量/属性。

So basically previous syntax: 所以基本上是以前的语法:

ItemSupplierName = (u != null) ? u.SupplierName : "";

Has moved to simplier version in C# 6: 已移至C#6的简化版本:

ItemSupplierName = u?.SupplierName ?? "";

As answered at other question , You can go get upgrade MS Build tools 2015 which supports C# 6 syntax & provides compiler for this. 正如在其他问题上回答的那样,您可以获取支持C#6语法并为此提供编译器的升级MS Build工具2015 Or You can also change the syntax to the previous version of C# to get it compiled. 或者,您也可以将语法更改为C#的早期版本以进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM