预处理指令

这些指令/命令不会转换为可执行代码,但会影响编译过程的各个方面;列如,可以让编译器不编译某一部分代码等。

C#中主要的预处理指令

#define和#undef

#define指令定义

#define DEBUG

它告诉编译器存在DEBUG这个符号;这个符号不是实际代码的一部分,而只是在编译器编译代码时候可能会根据这个符号做条件编译。

#undef定义:

#undef DEBUG

用来移除定义的符号DEBUG。如果不存在这样的标记,#undef指令则不会生效。同样,用#define再次定义一个同名的标记也不会有任何变化。

注意:

  1. 你需要将#define和#undef指令写在实际业务代码开始之前的位置。
  2. #define本身没有什么用,需要和其他预处理器指令结合使用;比如 #if

#if, #elif, #else和#endif

这些指令告诉编译器是否要编译包含在其中的代码块。例如:

int DoSomeWork(double x)
{
// do something
#if DEBUG
Console.WriteLine($"x is {x}");
#endif
}

这段代码中的Console.Writeline语句,只有在前面用#define指令定义了符号DEBUG后才会在编译的时候,真正被编译到;

如果编译器没发现DEBUG符号,就会在编译的时候忽略这句代码。 

#elif(= else if)和#else指令可以用在#if块中:

#define ENTERPRISE
#define W10
// further on in the file
#if ENTERPRISE
// do something
#if W10
// some code that is only relevant to enterprise
// edition running on W10
#endif
#elif PROFESSIONAL
// do something else
#else
// code for the leaner version
#endif

#if和#elif还支持有限的一些逻辑操作符,你可以用使用!,==,!=和||等。

一个标记如果存在,则认为是true,如果没有定义,就认为是false,因此你也可以这样使用:

#if W10 && (ENTERPRISE==false) // if W10 is defined but ENTERPRISE isn't

#warning和#error

当编译器遇到#warning的时候,会产生警告信息;

当编译器遇到#error的时候,会产生错误信息;

    class Program
{
static void Main(string[] args)
{ #warning this is a warning message which will be shown when compile Console.WriteLine("Hello World!"); #error this is a error message, and will break build
}
}

  编译结果:

Program.cs(10,10): warning CS1030: #warning: 'this is a warning message which will be shown when compile' [/define_warning/define_warning.csproj]
Program.cs(14,8): error CS1029: #error: 'this is a error message, and will break build' [/define_warning/define_warning.csproj]
1 Warning(s)
1 Error(s)

  使用这些指令可以检查#define语句是不是做错了什么事,使用#warning可以提醒要做些事情:

#if DEBUG && RELEASE
#error "You've defined DEBUG and RELEASE simultaneously!"
#endif
#warning "Don't forget to remove this line before the boss tests the code!"
Console.WriteLine("*I love this job.*");

  

#region和#endregion

可以用来标识一段代码,在Visual Studio或其他能够识别的IDE里比较有用。

#region Member Field Declarations
int x;
double d;
Currency balance;
#endregion

  

#line

#line指令可以用来改变编译器输出警告和错误时相应的文件名和行号信息。这个实际中,用的可能会比较少。

主要是在用第三方包的时候,有时候会导致编译器报告的行号或文件名与实际不匹配。

#line可以用于还原这种匹配。

#line 164 "Core.cs" // We happen to know this is line 164 in the file Core.cs,
// before the intermediate package mangles it.
// later on
#line default // restores default line numbering

  

#pragma

#pragma指令可以用来终止或恢复某个指定编号到编译器警告。

与命令行选项不同,#pragma指令可以在类或方法级别实现。

例如:

    class Program
{
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("Hello World!");
}
}

  编译是会有warning:

Program.cs(9,17): warning CS0219: The variable 'i' is assigned but its value is never used [/define_warning/define_warning.csproj]
1 Warning(s)
0 Error(s)  

从warning信息可以看出是warning CS0219,加入#pragma后就不会有warning了。

#pragma warning disable CS0219
public class Program
{
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("Hello World!");
}
}
#pragma warning restore CS0219

 注意:warning的代码是区分大小写的,CS2019要大写,如果写成cs2019则没有用。 

学习资料

C#高级编程(第11版) C# 7 & .NET Core 2.0(.NET开发经典名著)

最新文章

  1. (原)用pixi.js 实现 方块阵点击后原地自转效果
  2. Java(异常处理)动手动脑
  3. Boundaries
  4. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q131-Q132)
  5. SVN--(Eclipse)在历史记录中比较版本差异
  6. 《第一行代码》学习笔记11-活动Activity(9)
  7. MVC5 学习资料
  8. php memcached+Mysql(主从)
  9. springmvc返回值为void
  10. 实现Qt日志功能并输出到文件(qDebug\qWarning\ qCritical\qFatal)
  11. dubbo入门学习 一SOA
  12. xhr是什么文件类型?
  13. [laravel]malformed header from script 'index.php': Bad header: HTTP/1.1 302 Found, referer: http://localhost/auth/login
  14. Arcmap查找孤路
  15. C# WebAPI设置跨域
  16. IDEA 快速将spring boot项目打包成jar包,简单快速有效
  17. HttpFileCollection类
  18. Spring框架的JDBC模板技术概述
  19. JS应该放在什么位置?
  20. 如何解决Maven速度慢

热门文章

  1. jdk1.8的一些特性
  2. F. Machine Learning 带修端点莫队
  3. Gym101612H Hidden Supervisors
  4. Java并发(4)
  5. Pyqt5_QPushButton
  6. ngnix随笔三
  7. C# 数据操作系列 - 18 让Dapper更强的插件
  8. 那些面试官必问的JAVA多线程和并发面试题及回答
  9. Map接口之HashMap,LinkedHashMap,TreeMap
  10. Chisel3 - util - Pipe