微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/csharp/98343.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐