为什么空传播不一致地传播Nullable <T>?

Dav*_*ine 5 c# nullable c#-6.0 null-propagation-operator

我特别提到了对null传播的关注,因为它涉及到返回方法bool?的使用bool.例如,请考虑以下事项:

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute
{
    return property?.AttributeProvider
                    .GetAttributes(typeof(TAttribute), false)
                    .Any();
}
Run Code Online (Sandbox Code Playgroud)

这不编译,并存在以下错误:

不能隐含转换bool?布尔.存在显式转换(您是否错过了演员表)?

这意味着它是治疗方法的整个身体bool?,因为这样我会承担,我可以说.GetValueOrDefault().Any(),但这是不允许的.Any()回报bool不是bool?.

我知道我可以做以下任何一种解决方法:

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute
{
    return property?.AttributeProvider
                    .GetAttributes(typeof(TAttribute), false)
                    .Any()
        ?? false;
}
Run Code Online (Sandbox Code Playgroud)

要么

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute
{
    var any = 
        property?.AttributeProvider
                 .GetAttributes(typeof(TAttribute), false)
                 .Any();

     return any.GetValueOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

要么

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute
{
    return property?.AttributeProvider
                    .GetAttributes(typeof(TAttribute), false)
                    .Any()
        ?? false;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么我不能直接调用.GetValueOrDefault()链接.Any()调用?

public static bool IsAttributedWith<TAttribute>(this JsonProperty property) 
    where TAttribute : Attribute
{
    return (property?.AttributeProvider
                    .GetAttributes(typeof(TAttribute), false)
                    .Any())
                    .GetValueOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

我认为这是有道理的,因为价值实际上是bool?在这一点而不是bool.

use*_*407 6

经过?.运营商所有后续调用链解释为条件,不仅直接调用.所以,这段代码:

property?.AttributeProvider
         .GetAttributes(typeof(TAttribute), false)
         .Any()
Run Code Online (Sandbox Code Playgroud)

解释为

property==null ? (bool?)null : property.AttributeProvider
                                       .GetAttributes(typeof(TAttribute), false)
                                       .Any()
Run Code Online (Sandbox Code Playgroud)

如果你添加GetValueOrDefault():

property==null ? (bool?)null : property.AttributeProvider
                                       .GetAttributes(typeof(TAttribute), false)
                                       .Any()
                                       .GetValueOrDefault()
Run Code Online (Sandbox Code Playgroud)

它会失败,因为没有Any()回报.因此,您需要在此处使用括号:boolbool?

(property==null ? (bool?)null : property.AttributeProvider
                                        .GetAttributes(typeof(TAttribute), false)
                                        .Any())
                                        .GetValueOrDefault()
Run Code Online (Sandbox Code Playgroud)

使用?.运算符时需要使用的相同括号:

(property?.AttributeProvider
          .GetAttributes(typeof(TAttribute), false)
          .Any())
          .GetValueOrDefault()
Run Code Online (Sandbox Code Playgroud)