简体   繁体   English

为什么当我使用 !!= C# 时代码会被编译

[英]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.

相关问题 为什么连接调试器时,即时编译的C#代码不起作用? - Why does C# code compiled on the fly not work when a debugger is attached? 如果C#代码是JIT编译的,为什么我在构建时必须在Visual Studio中选择目标平台? - If C# code is JIT compiled why do I have to choose a target platform in Visual Studio when building? 为什么我写同样的算法时c#中的代码与c中的代码不同? - Why does the code in c# differs from the code in c when I write the same algorithm? 为什么我编译的 AutoIt 脚本在从 C# 调用时无法返回? - Why does my compiled AutoIt script fail to return when called from C#? Javascript中包含的C#代码是否仍需要编译? - Does C# code contained in Javascript still need to be compiled? 为什么此C#代码具有未声明的get主体构建? 以及如何使编译器对此产生错误? - Why does this C# code with an an undeclared get body build? And how can I make the compiler throw an error on this? 为什么C#RegexOptions.Compiled会使匹配变慢? - Why does the C# RegexOptions.Compiled makes the match slower? 如何在运行时编译的 C# 代码中使用“UnityEngine”? - How to use “UnityEngine” in runtime-compiled C# code? 为什么在我调用DataAdapter.Update()时此C#代码会生成语法错误? - Why does this C# code generate a Syntax Error when I call DataAdapter.Update()? 为什么我的 C# Xml 代码仅在我枚举变量可枚举时才有效 - Why does my C# Xml code only work when I enumerate variable enumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM