小天:我觉得警告这个东西,特别是我们自己定义的警告,可能有时候仅仅是为了编程中给自己提醒。诸如此类的警告其实并不影响程序,是否有什么办法可以抑制或者恢复制定的编译警告呢?
老田:有的,#pragma指令的作用就是这个。与命令行选项不同,#pragma指令可以在类或者方法上执行。对抑制警告的内容和抑制的时间进行更精细的控制。下面的例子就是禁止字段使用警告,然后在编译完当前类之后恢复该警告。
#pragma warning disable 169
classPragma_Test
{
int number;
}
#pragma warning restore 169
本文为天轰穿原著《C#教程》系列,转载请注明出处及作者!