合并可以为空的对象的属性?

Ber*_*ian 0 c# nullable

您好,有没有办法制定如下所示的条件?如果field为null则为false else field.Property?

class Node
{
    public bool IsFilled;
}

class Holder
{
    Node nodeBuffer;
    public bool IsFilled => this.nodeBuffer?.IsFilled ?? false; 
}
Run Code Online (Sandbox Code Playgroud)

我该怎么说if nodeBuffer is null then false else nodeBuffer.IsFilled呢?

Tim*_*ter 7

是的,您可以使用与之一起使用的相等运算符Nullable<bool>

public bool IsFilled => this.nodeBuffer?.IsFilled == true;
Run Code Online (Sandbox Code Playgroud)

可空类型支持所有非可空类型支持的运算符,称为:提升运算符