• C#的预处理指令


    C# 中所有预处理指令的详细说明和示例:

    1. #define:定义一个符号,可以在代码中使用该符号进行条件编译

      示例:

      #define DEBUG
    
       class Program
       {
           static void Main()
           {
       #if DEBUG
               Console.WriteLine("Debug mode");
       #endif
               Console.WriteLine("Normal mode");
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出:

    Debug mode
    Normal mode
    
    • 1
    • 2

    在上述示例中,使用 #define 定义了一个名为 DEBUG 的符号。在 Main 方法中,使用 #if#endif 来检查 DEBUG 符号是否定义,从而选择性地编译和执行代码块。

    1. #undef:取消定义一个符号,取消后该符号将不再被视为已定义。

      示例:

      #define DEBUG
    
       class Program
       {
           static void Main()
           {
       #if DEBUG
               Console.WriteLine("Debug mode");
       #endif
    
       #undef DEBUG
    
       #if DEBUG
               Console.WriteLine("This line will not be executed");
       #endif
    
               Console.WriteLine("Normal mode");
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    输出:

    Debug mode
    Normal mode
    
    • 1
    • 2

    在上述示例中,使用 #undef 取消了 DEBUG 符号的定义,因此第二个 #if DEBUG 块中的代码不会被执行。

    1. #if#elif#else#endif:用于条件编译,根据符号是否定义来选择性地编译和执行代码块。

      示例:

       #define DEBUG
    
       class Program
       {
           static void Main()
           {
       #if DEBUG
               Console.WriteLine("Debug mode");
       #elif RELEASE
               Console.WriteLine("Release mode");
       #else
               Console.WriteLine("No mode defined");
       #endif
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    输出:

    Debug mode
    
    • 1

    在上述示例中,根据 DEBUG 符号的定义情况,选择性地编译和执行不同的代码块。

    1. #warning:生成一个编译警告消息。

      示例:

       #warning This is a warning message
    
       class Program
       {
           static void Main()
           {
               Console.WriteLine("Hello, world!");
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:

    Hello, world!
    
    • 1

    #warning 预处理指令用于生成编译器警告消息。当编译器遇到 #warning 指令时,它会生成一个警告消息,其中包含指定的文本。

    在这种情况下,#warning This is a warning message 指令会生成一个警告消息,内容为 “This is a warning message”。

    然而,警告消息只是编译器生成的消息,不会影响代码的执行。因此,无论是否生成了警告消息,执行 Console.WriteLine(“Hello, world!”); 这行代码时,它将打印出 “Hello, world!”。

    所以,最终输出结果是 “Hello, world!”。同时,编译器会生成一个警告消息,内容为 “This is a warning message”。

    1. #error:生成一个编译错误消息。

      示例:

       #if DEBUG
       #error Debug mode is not supported
       #endif
    
       class Program
       {
           static void Main()
           {
               Console.WriteLine("Hello, world!");
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在上述示例中,如果 DEBUG 符号被定义,则会生成一个编译错误消息,并阻止代码的编译。

    1. #region#endregion:用于在代码中创建可折叠的区域,提高代码的可读性。

      示例:

       class Program
       {
           #region Constants
           private const int MaxValue = 100;
           private const int MinValue = 0;
           #endregion
    
           static void Main()
           {
               int value = GetValue();
               if (value > MaxValue)
               {
                   Console.WriteLine("Value exceeds maximum value.");
               }
               else if (value < MinValue)
               {
                   Console.WriteLine("Value is below minimum value.");
               }
               else
               {
                   Console.WriteLine("Value is within the valid range.");
               }
           }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在上述示例中,使用 #region#endregion 创建了一个名为 “Constants” 的可折叠区域,用于组织和隐藏常量定义。

    1. #line:指定编译器输出错误和警告消息的行号和文件名。

      示例:

      #line 200 "CustomFile.cs"
      Console.WriteLine("This line is from CustomFile.cs");
      
      #line default
      Console.WriteLine("This line is from the default file");
      
      #line hidden
      Console.WriteLine("This line is hidden from the compiler");
      
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

    #line 预处理指令用于指定编译器输出错误和警告消息的行号和文件名。它可以用于调整编译器生成的行号和文件名,以便更准确地指示编译器错误和警告的位置。

    #line 指令有三种不同的用法:

    1. #line number
      这种用法将指定当前行号。编译器将使用指定的行号作为下一个代码行的行号,并将当前文件名作为默认文件名。

      示例:

       #line 200
       Console.WriteLine("This line has line number 200");
    
       Console.WriteLine("This line has line number 201");
    
    • 1
    • 2
    • 3
    • 4

    在上述示例中,#line 200 指定了当前行号为 200,因此下一行代码的行号将被设置为 200。

    1. #line number "filename"
      这种用法将指定当前行号和文件名。编译器将使用指定的行号和文件名作为下一个代码行的行号和文件名。

      示例:

       #line 200 "CustomFile.cs"
       Console.WriteLine("This line is from CustomFile.cs");
    
       Console.WriteLine("This line is from the default file");
    
    • 1
    • 2
    • 3
    • 4

    在上述示例中,#line 200 "CustomFile.cs" 指定了当前行号为 200,文件名为 “CustomFile.cs”。这将影响下一行代码的行号和文件名。

    1. #line default
      这种用法将恢复默认的行号和文件名。编译器将使用默认的行号和文件名作为下一个代码行的行号和文件名。

      示例:

       #line 200 "CustomFile.cs"
       Console.WriteLine("This line is from CustomFile.cs");
    
       #line default
       Console.WriteLine("This line is from the default file");
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在上述示例中,#line default 恢复了默认的行号和文件名,使得下一个代码行使用默认的行号和文件名。

    #line 指令对于在特定情况下生成动态生成的代码或报告错误时非常有用。它允许开发人员控制编译器输出的行号和文件名,使得错误和警告消息更准确地指示问题所在。

    请注意,#line 指令仅影响编译器生成的错误和警告消息的行号和文件名,不会对实际的代码行号产生任何影响。它只是一种控制编译器行为的指令,不会影响代码的运行时行为。

    #line hidden 预处理指令用于隐藏一段代码的行号和文件名信息,从而在编译器生成的错误和警告消息中不显示该段代码的位置信息。

    当使用 #line hidden 指令时,编译器会将接下来的代码行视为隐藏的,并将它们的行号和文件名信息从编译器生成的错误和警告消息中省略。

    下面是一个示例:

    #line hidden
    Console.WriteLine("This line is hidden");
    
    // Some other code
    
    Console.WriteLine("This line is not hidden");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在上述示例中,#line hidden 指令将 Console.WriteLine("This line is hidden"); 这一行代码及其之后的代码行标记为隐藏。这意味着,该行及其后续行的行号和文件名信息将在编译器生成的错误和警告消息中被隐藏。

    因此,即使在隐藏的代码行中存在错误或警告,编译器生成的消息也不会显示相应的行号和文件名信息。相反,错误和警告消息将只指示隐藏的代码段中存在问题,而不提供具体的位置信息。

    需要注意的是,#line hidden 只是控制编译器生成的错误和警告消息中的行号和文件名信息,它并不影响代码的执行或其他语义。隐藏的代码行仍然会被编译和执行,只是在生成的消息中不会显示其位置信息。

  • 相关阅读:
    【Kafka】Golang中使用Kafka基于发布订阅模式实现消息队列
    【好书分享第十一期】深入Rust标准库(文末送书)
    R语言和医学统计学(13):协方差分析
    Linux ——目录结构
    【云原生之k8s】kubernetes核心组件
    HC小区管理系统房屋收费功能说明
    Unity的WebGL平台动态加载glb/gltf测试
    网工知识角|轻松拿offer【网工面试题】三层交换机与路由器有什么区别?
    Vue2监听浏览器可视区域的大小,
    vue中关于表单的常用学习
  • 原文地址:https://blog.csdn.net/ultramand/article/details/134085876