Visual Studio中的语法错误与编译器错误,或红色波浪下划线与蓝色波浪下划线

时间:2021-05-12 22:42:09

What is the difference between a "syntax error" and a "compiler error" as Visual Studio sees it? Or, put another way, why are some "compile-time" errors underlined with red wavy lines and some with blue wavy lines? Here is an example:

Visual Studio看到的“语法错误”和“编译器错误”有什么区别?或者换句话说,为什么一些“编译时”错误用红色波浪线和一些蓝色波浪线加下划线?这是一个例子:

Visual Studio中的语法错误与编译器错误,或红色波浪下划线与蓝色波浪下划线

The red underlined error above has this description:

上面的红色下划线错误有以下描述:

No overload for method 'ValidateFilteredRecipient' takes 6 arguments

方法'ValidateFilteredRecipient'没有重载需要6个参数

The blue underlined error has this description:

蓝色下划线错误有这样的描述:

'ValidateBuild': cannot declare instance members in a static class

'ValidateBuild':不能在静态类中声明实例成员

It's not clear to me what the distinguishing characteristics of the two errors are.

我不清楚这两个错误的区别特征是什么。

I thought finding the answer would be a piece of cake: I'd just google it and the first result would be an MSDN page expounding this topic fully; however, very surprisingly, this was not the case. I started off by trying to google the colors (since I didn't yet know red meant "syntax error" and blue meant "compiler error"):

我认为找到答案将是一块蛋糕:我只是谷歌它,第一个结果将是一个MSDN页面完全阐述这个主题;然而,非常令人惊讶的是,事实并非如此。我开始试图谷歌的颜色(因为我还不知道红色意味着“语法错误”,蓝色意味着“编译器错误”):

visual studio red underline vs. blue underline

视觉工作室红色下划线与蓝色下划线

No help there. Then I tried these searches:

没有帮助。然后我尝试了这些搜索:

visual studio error underline color meanings
visual studio underline color meanings

视觉工作室错误强调颜色含义视觉工作室强调颜色含义

I could see that this was going nowhere, so I googled a bit more and figured out where the colors were set in VS: Tools > Options > Environment > Fonts and Colors. By the way, the inability to search the huge list here is extremely annoying, but I figured out that "syntax errors" have the red wavy underline, and "compiler errors" have the blue wavy underline.

我可以看到这无处可去,所以我搜索了一下,找出了在VS中设置颜色的位置:工具>选项>环境>字体和颜色。顺便说一句,无法在这里搜索巨大的列表是非常烦人的,但我发现“语法错误”有红色波浪下划线,“编译器错误”有蓝色波浪下划线。

Visual Studio中的语法错误与编译器错误,或红色波浪下划线与蓝色波浪下划线

So what do those mean? Back to Google:

那是什么意思?回到Google:

visual studio compiler error vs. syntax error

visual studio编译器错误与语法错误

Nothing relevant. Here's the closest I've found, from MSDN:

没什么关系的。这是我从MSDN找到的最接近的:

Fonts and Colors, Environment, Options Dialog Box

字体和颜色,环境,选项对话框

This page has these entries:

此页面包含以下条目:

Compiler Error -- Blue squiggles in the editor indicating a compiler error.

编译器错误 - 编辑器中的蓝色波形表示编译器错误。

and

Syntax Error -- Parse errors.

语法错误 - 解析错误。

Not at all helpful -- emphasis on "at all". I have always thought I knew what a "syntax error" was, at least, and Wikipedia agrees:

根本没有帮助 - 强调“根本”。我一直以为我知道什么是“语法错误”,至少,*同意:

a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language.

语法错误是用于以特定编程语言编写的字符序列或标记的语法中的错误。

Also, here is what what it has for syntax:

此外,这是它的语法:

the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language.

计算机语言的语法是一组规则,用于定义被认为是该语言中正确结构化的文档或片段的符号组合。

So, going back to my "syntax error" from above:

所以,从上面回到我的“语法错误”:

No overload for method 'ValidateFilteredRecipient' takes 6 arguments

方法'ValidateFilteredRecipient'没有重载需要6个参数

How is that a syntax error per the definition I've included here? Actually, in my opinion, the "compiler error" I got more-closely meets this definition of a syntax error:

根据我在这里包含的定义,这是一个语法错误怎么样?实际上,在我看来,“编译器错误”我更加密切地满足了语法错误的定义:

'ValidateBuild': cannot declare instance members in a static class

'ValidateBuild':不能在静态类中声明实例成员

Can someone please help me figure this out?

有人可以帮我解决这个问题吗?

2 个解决方案

#1


1  

The key difference between the syntax error and compilation error is when Visual Studio detects it.

语法错误和编译错误之间的关键区别在于Visual Studio检测到它。

Syntax error is detected and highlighted as you code. You do not have to build the code to get these errors.

在编码时检测到语法错误并突出显示。您不必构建代码来获取这些错误。

However, Compilation errors are complex that Editor cannot detect as you code. You will have to run it through a compiler (do a build) to identify them. So the chances are one might end up continue coding without compiling and will not be able to detect them until he builds.

但是,编译错误很复杂,编辑在编码时无法检测到。您必须通过编译器(执行构建)运行它来识别它们。因此,有可能最终会在没有编译的情况下继续编码,并且在构建之前无法检测到它们。

Basically, Syntax errors are a subset of compilation errors. If you use text editor like notepad to write your code, you will never see syntax error. All will be logged as Compilation errors when you run the code through compiler.

基本上,语法错误是编译错误的一个子集。如果您使用文本编辑器(如记事本)编写代码,您将永远不会看到语法错误。通过编译器运行代码时,所有都将记录为编译错误。

In regards to highlighting them differently, I think it is a visual representation for the Developer to understand what he could have caught this while writing the code.

关于以不同方式突出显示它们,我认为这是开发人员在编写代码时理解他可以捕到的内容的直观表示。

As mentioned by everyone, at the end of day, you need to fix both to make you code run.

正如大家所提到的那样,在一天结束时,您需要修复两者以使代码运行。

This is true for any programming language and the editor used along with that language.

对于任何编程语言和与该语言一起使用的编辑器都是如此。

#2


3  

Both Syntax and compiler errors will prevent your code from compiling.

语法和编译器错误都会阻止您的代码编译。

Syntax Errors refer to how your code interacts with other pieces of code. Things like type mismatches when passing parameters to functions etc.

语法错误是指代码如何与其他代码进行交互。将参数传递给函数等时类型不匹配的事情。

Compiler errors on the other hand refer to more fundamental architectural violations like trying to inherit from sealed classes or defining non static members inside static classes. These go against the definition of the language rather than the usage of the language.

另一方面,编译器错误指的是更基本的架构违规,例如尝试从密封类继承或在静态类中定义非静态成员。这违背了语言的定义而不是语言的用法。

#1


1  

The key difference between the syntax error and compilation error is when Visual Studio detects it.

语法错误和编译错误之间的关键区别在于Visual Studio检测到它。

Syntax error is detected and highlighted as you code. You do not have to build the code to get these errors.

在编码时检测到语法错误并突出显示。您不必构建代码来获取这些错误。

However, Compilation errors are complex that Editor cannot detect as you code. You will have to run it through a compiler (do a build) to identify them. So the chances are one might end up continue coding without compiling and will not be able to detect them until he builds.

但是,编译错误很复杂,编辑在编码时无法检测到。您必须通过编译器(执行构建)运行它来识别它们。因此,有可能最终会在没有编译的情况下继续编码,并且在构建之前无法检测到它们。

Basically, Syntax errors are a subset of compilation errors. If you use text editor like notepad to write your code, you will never see syntax error. All will be logged as Compilation errors when you run the code through compiler.

基本上,语法错误是编译错误的一个子集。如果您使用文本编辑器(如记事本)编写代码,您将永远不会看到语法错误。通过编译器运行代码时,所有都将记录为编译错误。

In regards to highlighting them differently, I think it is a visual representation for the Developer to understand what he could have caught this while writing the code.

关于以不同方式突出显示它们,我认为这是开发人员在编写代码时理解他可以捕到的内容的直观表示。

As mentioned by everyone, at the end of day, you need to fix both to make you code run.

正如大家所提到的那样,在一天结束时,您需要修复两者以使代码运行。

This is true for any programming language and the editor used along with that language.

对于任何编程语言和与该语言一起使用的编辑器都是如此。

#2


3  

Both Syntax and compiler errors will prevent your code from compiling.

语法和编译器错误都会阻止您的代码编译。

Syntax Errors refer to how your code interacts with other pieces of code. Things like type mismatches when passing parameters to functions etc.

语法错误是指代码如何与其他代码进行交互。将参数传递给函数等时类型不匹配的事情。

Compiler errors on the other hand refer to more fundamental architectural violations like trying to inherit from sealed classes or defining non static members inside static classes. These go against the definition of the language rather than the usage of the language.

另一方面,编译器错误指的是更基本的架构违规,例如尝试从密封类继承或在静态类中定义非静态成员。这违背了语言的定义而不是语言的用法。