? LINQ中使用的运算符

时间:2017-10-01 05:31:20

标签: c# linq

我刚开始学习LINQ,然后只是坚持教程中提供的示例语句(请注意,我不是在使用C#.Net Framework)。声明

#include<iostream>
#include"guest.h"

using namespace std;

Guest::Guest()
{

    strcpy_s(firstName,"???");
    strcpy_s(lastName, "???");

}

char* Guest::getFirstName()
{
    return firstName;
}
char* Guest::getLastName()
{
    return lastName;
}

只有在 arr?.Count(w => w != null) > 0 (数组或列表)中至少有一个非空元素时才返回True。但arr运营商在那里做什么呢?这是另一种形式或三元运算符还是别的什么?请分享您关于这一点的宝贵知识。我将很高兴并感谢您阅读好的答案。

注意:我尝试删除语句中的?运算符,但找不到任何区别。

1 个答案:

答案 0 :(得分:2)

这是Null conditional operator

它基本上检查null并且如果不为null则执行条件。在arr为空的情况下,此代码不会引发异常。如果在没有Null条件运算符的情况下编写,则会得到NullReferenceException

// this would throw an exception
int?[] arr;
arr.Count(w => w != null) > 0;

// this will check if arr is null and not proceed to call the .Count method
int?[] arr;
arr?.Count(w => w != null) > 0;