zoukankan      html  css  js  c++  java
  • C#预处理器指令 -0016

    预处理指令

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

    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开发经典名著)

  • 相关阅读:
    Python 知识要点:函数
    软件测试
    软件测试
    软件测试
    SpringBoot框架
    Redis存储系统
    Linux系统
    maven
    shiro安全框架
    Mybatis框架
  • 原文地址:https://www.cnblogs.com/codesee/p/13096327.html
Copyright © 2011-2022 走看看