Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#中的#line指令更改错误或警告';谁的默认行号?_C#_Preprocessor Directive - Fatal编程技术网

使用c#中的#line指令更改错误或警告';谁的默认行号?

使用c#中的#line指令更改错误或警告';谁的默认行号?,c#,preprocessor-directive,C#,Preprocessor Directive,我正在阅读微软关于C语言中的行指令的网页。它解释了#line 200指令强制下一行的编号为200(尽管默认为#6),直到#line default指令将行号返回到其默认编号为止 我的问题是,究竟为什么我们需要更改代码中错误或警告的行号 class MainClass { static void Main() { #line 200 "Special" int i; int j; #line default cha

我正在阅读微软关于C语言中的行指令的网页。它解释了#line 200指令强制下一行的编号为200(尽管默认为#6),直到#line default指令将行号返回到其默认编号为止

我的问题是,究竟为什么我们需要更改代码中错误或警告的行号

class MainClass
{
    static void Main()
    {
#line 200 "Special"
        int i;
        int j;
#line default
        char c;
        float f;
#line hidden // numbering not affected
        string s;
        double d;
    }
}
编译生成以下输出:

Special(200,13): warning CS0168: The variable 'i' is declared but never used
Special(201,13): warning CS0168: The variable 'j' is declared but never used
MainClass.cs(9,14): warning CS0168: The variable 'c' is declared but never used
MainClass.cs(10,15): warning CS0168: The variable 'f' is declared but never used
MainClass.cs(12,16): warning CS0168: The variable 's' is declared but never used
MainClass.cs(13,16): warning CS0168: The variable 'd' is declared but never used

这条指令在很多情况下都很有用,大多数C#开发者从未见过

我所知道的所有场景都涉及一个由其他进程生成或修改的C文件,而
#line
指令用于确保文件编译版本的.pdb信息提供与原始输入相关的准确信息

用他们的例子来暗示这一点。他们设想一个构建步骤,在编译结果之前从原始C#文件中删除(删除)行。如果调试信息显示的是已处理的行号而不是原始行号,则尝试调试该代码的人将遇到困难,因为当他们进行调试时,他们可能会查看原始编写的文件,而不是修改后的预处理文件


我知道的其他示例包括T4处理的输出,以及编译XAML文件时生成的C#文件。在这些情况下,没有原始C#文件,但有原始编写的文件,如果有异常或其他原因将行号传递给试图调试代码的人,他们将希望从原始文件中知道行号,而不是他们可能永远看不到的生成文件。

有趣的问题。做了一个谷歌搜索发现。CodeGen工具看起来像是这方面的一个大消费者。如果你需要它,你就会知道你需要它。问别人如何使用勺子是毫无意义和徒劳的,有很多用途。文档中的注释也给了你一个完美的例子谢谢你@Steve,这是一个有用的链接。@MichaelRandall,阅读这篇文章是我c#课程的一项作业。我相信如果我不知道学习的原因,我很快就会忘记,这将是毫无意义的。这就是我问这个问题的原因。谢谢你的评论。