加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:22:50 所属栏目:百科 来源:网络整理
导读:为什么非关闭的#region导致编译器错误?毕竟,该区域本身对编译的代码完全没有影响,对吧? 我相信这是因为它是一个预处理器指令,但为什么呢?毕竟它不像其他指令那样使用. class Application{ #region Whatever - Causes an error. static void Main(string[]
为什么非关闭的#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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读