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

Syntax error versus compiler error in Visual Studio, or red wavy underline versus blue wavy underline

"syntax error" 和 Visual Studio 眼中的 "compiler error" 有什么区别?或者,换句话说,为什么有些 "compile-time" 错误用 red 波浪线下划线,而有些用 blue 波浪线加下划线?这是一个例子:

上面红色下划线的错误是这样描述的:

No overload for method 'ValidateFilteredRecipient' takes 6 arguments

带蓝色下划线的错误描述如下:

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

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

我认为找到答案是小菜一碟:我只是 google 它,第一个结果将是一个 MSDN 页面,充分阐述了这个主题;然而,非常令人惊讶的是,事实并非如此。我开始尝试 google 颜色(因为我还不知道红色表示 "syntax error" 而蓝色表示 "compiler error"):

visual studio red underline vs. blue underline

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

visual studio error underline color meanings
visual studio underline color meanings

我发现这无济于事,所以我 google 进一步研究并弄清楚了在 VS 中设置颜色的位置:工具 > 选项 > 环境 > 字体和颜色。顺便说一句,无法在这里搜索巨大的列表非常烦人,但我发现 "syntax errors" 有红色波浪下划线,"compiler errors" 有蓝色波浪下划线。

那么这些是什么意思?返回 Google:

visual studio compiler error vs. syntax error

没有相关内容。这是我从 MSDN 中找到的最接近的:

Fonts and Colors, Environment, Options Dialog Box

此页面包含这些条目:

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

Syntax Error -- Parse errors.

完全没有帮助 -- 强调 "at all"。我一直认为我至少知道 "syntax error" 是什么,并且 Wikipedia 同意:

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.

此外,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.

所以,回到上面的 "syntax error":

No overload for method 'ValidateFilteredRecipient' takes 6 arguments

根据我在此处包含的定义,这怎么会是语法错误?实际上,在我看来,我得到的 "compiler error" 更符合语法错误的定义:

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

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

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

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

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

语法错误和编译错误的主要区别在于 Visual Studio 检测到它的时间。

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

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

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

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

正如大家所提到的,在一天结束时,您需要同时解决这两个问题才能使您的代码 运行。

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