[英]use of ?? to get attributes of an object if the object isn't null
Is it possible to use C# '??' 是否可以使用C#'??' operator as this :
运算符如下:
var _var = pr.Value ?? "NA";
To check if pr is null and not if Value attribute of pr is null ? 要检查pr是否为null,而不是pr的Value属性是否为null?
I don't find any example that show something like this. 我找不到任何显示这样的例子。
If yes, is it recommanded or is it better to use extension method in this case ? 如果是,在这种情况下是否建议使用扩展方法?
There are 3 different scenarios here so let's go through the syntax for each: 这里有3种不同的情况,因此让我们仔细研究每种情况的语法:
Return "NA"
if either pr
or pr.Value
is null: 返回
"NA"
如果任一 pr
或pr.Value
为null:
var _var = pr?.Value ?? "NA";
The ?.
?.
operator will basically say that "if pr
is null, return null for the whole expression and do not dereference pr.Value
" 运算符将基本上说“如果
pr
为null,则为整个表达式返回null,并且不取消引用pr.Value
”
So if pr
is null, the ?.
因此,如果
pr
为null,则?.
operator will make the expression evaluate to null, or if pr.Value
is null, it will also evaluate to null, and in both cases, ?? "NA"
运营商将表达计算为NULL,或者如果
pr.Value
为null,它也将评估为空,而在这两种情况下, ?? "NA"
?? "NA"
will evaluate the whole thing to "NA"
. ?? "NA"
会将整个结果评估为"NA"
。
This is result-wise equivalent to this: 在结果上等效于此:
string _var; if (pr is null || pr.Value is null) _var = "NA"; else _var = pr.Value;
Return "NA"
if pr.Value
is null: 如果
pr.Value
为null,则返回"NA"
:
var _var = pr.Value ?? "NA";
This is what you have. 这就是你所拥有的。 This will throw a
NullReferenceException
if pr
is null, but evaluate to "NA"
if pr
is non-null but pr.Value
is null. 如果
pr
为null,则将抛出NullReferenceException
,但如果pr
为非null但pr.Value
为null,则计算结果为"NA"
。
This is result-wise equivalent to this: 在结果上等效于此:
string _var; if (pr.Value is null) // can throw NullReferenceException _var = "NA"; else _var = pr.Value;
Return "NA"
if pr
is null and ... what if pr.Value
is null? 如果
pr
为null,则返回"NA"
如果pr.Value
为null,该怎么办?
Neither ??
都不
??
nor ?.
也不
?.
can help you here but you can use the ternary operator: 可以在这里为您提供帮助,但是您可以使用三元运算符:
var _var = (pr is null) ? "NA" : pr.Value;
This will evaluate to "NA"
if pr
is null, pr.Value
if pr
is non-null, which will thus be able to evaluate to null if pr
is non-null but pr.Value
is null. 这将评估为
"NA"
如果pr
为null, pr.Value
如果pr
是不为空,因此这将能够评估为空,如果pr
为非null但pr.Value
为空。
Note , the ??
注意 ,
??
operator is also evaluated lazily, just as the ternary operator, so if you have this: 运算符也像三元运算符一样被懒惰地求值,因此如果您有以下情况:
var _v = pr?.Value ?? ComputeTheValue();
and that method call is expensive, it will only be evaluated if pr?.Value
is actually null, if pr
is non-null and pr.Value
is non-null, the call will not be made. 并且该方法调用很昂贵,只有在
pr?.Value
实际上为null时,才会进行评估,如果pr
为非null且pr.Value
为非null,则不会进行调用。
To check if pr is null and not if Value attribute of pr is null ?
要检查pr是否为null,而不是pr的Value属性是否为null ?
Seems like you only want to check if pr
is null. 似乎您只想检查
pr
是否为null。 in which case ternary operator is your option: 在这种情况下,三元运算符是您的选择:
var _var = pr == null ? "NA" : pr.Value;
or use a simple if
/ else
. 或使用简单的
if
/ else
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.