为什么#region 会产生编译错误?

Why does #region create compiling errors?

为什么非闭合 #region 会导致编译器错误?毕竟,区域本身对编译后的代码绝对没有影响,对吧?

我相信这是因为它是一个预处理器指令,但为什么呢?毕竟它不像其他指令那样使用。

class Application
{
    #region Whatever <- Causes an error.
    static void Main(string[] c)
    {

    }

}

为什么不能忽略它?或者是否有一个我不知道的关于 #region 的特性解释了它被编译的原因?

Edit :我知道这个示例无法编译,因为没有对应的 #endregion。但它不应该被视为 xml 评论中缺少的结束标记吗?我的意思是,它具有同样的重要性,不是吗?

第二次编辑:我想了解将 #region 设为预处理器指令背后的设计决策。为什么不只是 IDE 将识别为可以折叠的代码区域的一种注释?

Why does a non-closing #region causes a compiler error?

因为specs这么说。

A #region block must be terminated with a #endregion directive.

是的,它们不会成为编译程序的一部分,但其他预处理器指令可以定义,什么必须成为程序的一部分,例如 #if DEBUG (包括一些东西)

2.5 Pre-processing directives

Pre-processing directives are not tokens and are not part of the syntactic grammar of C#. However, pre-processing directives can be used to include or exclude sequences of tokens and can in that way affect the meaning of a C# program.

针对您的问题:

But why is the preprocessor even considering the #region tag, why was it created as a preprocessor directive and not just another kind of comment?

区域有助于 IDE (Visual Studio) 目的,(如果不是不可能的话) 很难达到 collapsing/expanding 没有区域的代码。 (这是我的猜测,只有微软的开发团队成员可以回答这个问题)

Region directive:

No semantic meaning is attached to a region; regions are intended for use by the programmer or by automated tools to mark a section of source code. The message specified in a #region or #endregion directive likewise has no semantic meaning; it merely serves to identify the region. Matching #region and #endregion directives may have different pp-messages.