Why does null propagation inconsistently apply to Nullable <T>?
I specifically draw attention to zero propagation, since it also applies to the bool?use of the return method bool. For example, consider the following:
public static bool IsAttributedWith<TAttribute>(this JsonProperty property)
where TAttribute : Attribute
{
return property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any();
}
This does not compile, and the following error exists:
Can't implicitly convert bool? cheat. Does explicit conversion exist (are you skipping listing)?
This means that it treats the entire method object as bool?, so I could assume that after .Any()I could say .GetValueOrDefault(), but this is not allowed, since it .Any()returns boolnot bool?.
I know that I could do one of the following:
public static bool IsAttributedWith<TAttribute>(this JsonProperty property)
where TAttribute : Attribute
{
return property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any()
?? false;
}
or
public static bool IsAttributedWith<TAttribute>(this JsonProperty property)
where TAttribute : Attribute
{
var any =
property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any();
return any.GetValueOrDefault();
}
or
public static bool IsAttributedWith<TAttribute>(this JsonProperty property)
where TAttribute : Attribute
{
return property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any()
?? false;
}
: .GetValueOrDefault() .Any()?
public static bool IsAttributedWith<TAttribute>(this JsonProperty property)
where TAttribute : Attribute
{
return (property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any())
.GetValueOrDefault();
}
, , bool? , bool.
+4
2
?. . , :
property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any()
property==null ? (bool?)null : property.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any()
GetValueOrDefault():
property==null ? (bool?)null : property.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any()
.GetValueOrDefault()
, Any() bool bool?. , :
(property==null ? (bool?)null : property.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any())
.GetValueOrDefault()
, , ?.:
(property?.AttributeProvider
.GetAttributes(typeof(TAttribute), false)
.Any())
.GetValueOrDefault()
+6