c# – 为什么#region会创建编译错误?

为什么非关闭的#region导致编译器错误?毕竟,该区域本身对编译的代码完全没有影响,对吧?

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

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

    }

}

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

编辑:我得到的是这个例子没有编译,因为没有相应的#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)的目的,在没有区域的情况下实现折叠/扩展代码的相同功能将非常困难(如果不是不可能的话). (这是我的猜测,只有微软开发团队的成员才能回答这个问题)

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.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...