[英]Why does the code get compiled when I use !!= C#
I am trying to understand how does the code get compiled when I use (!!=) Apparently the 2 snippets below do the same thing.我试图了解当我使用 (!!=) 时代码是如何编译的,显然下面的 2 个片段做同样的事情。 Why are both permissable?为什么两者都允许?
if (4 !!= 5)
Console.WriteLine("vvvvvv");
the above does the same thing as:以上与以下内容相同:
if (4 != 5)
Console.WriteLine("vvvvvv");
The expression 4 !!= 5
is parsed as the null-forgiving operator applied to 4, and then !=
applied to that expression and 5. That is, (4!) != 5
.表达式4 !!= 5
被解析为应用于 4 的容空运算符,然后!=
应用于该表达式和 5。即(4!) != 5
。
According to the draft spec , a null forgiving expression is a kind of primary expression:根据草案规范,null forgiving 表达式是一种主要表达式:
primary_expression
: ...
| null_forgiving_expression
;
null_forgiving_expression
: primary_expression '!'
;
and that:然后:
The postfix
!
后缀!
operator has no runtime effect - it evaluates to the result of the underlying expression.运算符没有运行时影响 - 它计算底层表达式的结果。 Its only role is to change the null state of the expression to "not null", and to limit warnings given on its use.它的唯一作用是将表达式的 null 状态更改为“not null”,并限制对其使用发出的警告。
In other words, the !
换句话说, !
after 4 does nothing and is very redundant. 4 之后什么都不做,非常多余。 The constant 4 is never null after all :)毕竟常数 4 永远不会为空:)
This only works in C# 8.0 and later.这仅适用于 C# 8.0 及更高版本。 See null-forgiving .请参阅null-forgiving 。
I believe you are just stating that 4 could be null and telling the compiler that it should not show errors if 4 does happen to be null.我相信您只是在说明 4 可能为空,并告诉编译器如果 4 确实为空,它不应该显示错误。
To complement other answers - in case you don't understand something about what is happening in terms of compilation you can use tools such decompilers - for example an online one - https://sharplab.io/ .为了补充其他答案 - 如果您不了解编译方面发生的事情,您可以使用反编译器等工具 - 例如在线工具 - https://sharplab.io/ 。 Among the others capabilities it provides ability to see the decompiled to IL (not very useful here), C# (basically desugared version of the code, for this one - see , also not very useful here) and also syntax tree (for this one - see ), which can be useful in this particular case.在其他功能中,它提供了查看反编译为IL (这里不是很有用)、C#(基本上是代码的脱糖版本,对于这个 - 见,在这里也不是很有用)和语法树(对于这个 - 请参阅),这在这种特殊情况下可能很有用。 I've used next code (so it can be compiled in release mode without optimizing constants out):我使用了下一个代码(因此它可以在发布模式下编译而无需优化常量):
public class C {
public void M(int? i) {
if (i !!= 5)
Console.WriteLine("vvvvvv");
}
}
If you expand CompilationUnit -> ClassDeclaration -> MethodDeclaration -> Body -> IfStatement -> Condition -> Left you will see that it is actually SuppressNullableWarningExpression
with operand being i
:如果展开 CompilationUnit -> ClassDeclaration -> MethodDeclaration -> Body -> IfStatement -> Condition -> Left 你会看到它实际上是SuppressNullableWarningExpression
操作数为i
:
With sharplab.io kindly highlighting the part of the code which is represented by selected syntax node.使用Sharplab.io,请突出显示由所选语法节点表示的代码部分。 So as others described you can see that compiler parses your code as 4
followed by null-forgiving operator.因此,正如其他人所描述的,您可以看到编译器将您的代码解析为4
后跟 null-forgiving 运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.