C# 中的预处理器指令

C# 中的预处理器指令告诉编译器在程序实际编译开始之前处理给定的信息。它以井号(#)开头,而且由于这些预处理器指令不是语句,所以末尾不附加分号。C# 编译器没有单独的预处理器,但这些指令的处理方式就好像存在单独的预处理器一样。除了预处理器指令之外,一行中不能包含其他任何内容。

C# 中使用的预处理器指令如下表所示:

预处理器指令

描述

#define

用于定义一个符号(Symbol)

#undef

取消符号的定义

#if

检查符号是否计算为 true

#endif

结束以 #if 开始的条件指令

#else

如果 #if 的符号值计算为 false,则执行 #else 指令语句

#elif

创建一个复合条件指令,如果符号值为 true 则执行

#error

创建一个用户定义的错误

#warning

创建一个用户定义的警告

#line

修改编译器的默认行号

#region

指定可以展开或折叠的代码块

#endregion

指定区域的结束

#pragma

为文件的编译向编译器提供信息

#pragma warning

用于启用或禁用警告

#pragma checksum

为源文件创建校验和示例 1:使用 #define, #if, #else 和 #endif 让我们通过几个例子来理解这个概念。在下面给出的代码中,我们使用 #define 定义了一个名为 shape 的符号。这意味着 ‘shape‘ 的计算结果为 true。在 main 函数内部,我们使用 #if 检查 shape 是否存在。由于它确实存在,并且编译器事先就知道这一点,所以 #else 部分永远不会被执行,并被编译器视为注释。#endif 用于指示 if 指令的结束。

    // C# Program to show the use of
    // preprocessor directives

    // Defining a symbol shape
    #define shape

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

    namespace Preprocessor {

    class Program {

        static void Main(string[] args)
        {
            // Checking if symbol shape exists or not
            #if (shape)
               Console.WriteLine("Shape Exists");
            #else
               Console.WriteLine("Shape does not Exist");

           // Ending the if directive
           #endif
        }
    }
    }
    

输出:

Shape Exists

示例 2:使用 #warning 和 #define 让我们看另一个例子。在下面给出的代码中,我们有意移除了符号 shapeshape_ 的定义。由于编译器找不到这些符号,它将执行 #else 指令。在这里,我们生成用户定义的警告和错误。

    // C# program to show the Removal of
    // definition of shape and shape_

    #undef shape_

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

    namespace preprocessor2
    {
        class Program
        {
            static void Main( string[] args )
            {
                // Checking if shape exists
                #if (shape)
                    Console.WriteLine("Shape Exists");
                    
                // Or if shape_ exists
                #elif (shape_)
                    Console.WriteLine("Shape_ Exists" );
                    
                #else

                // using #warning to display message that
                // none of the symbols were found
                #warning "No Symbols found"

                // Generating user defined error
                #error "Check use of preprocessors"

                // Ending if
                #endif

            }
        }
    }
    

这段代码将无法编译,因为代码中存在错误。警告和错误本质上做的是同样的工作,但错误会停止代码的编译过程。Visual Studio 将给出以下结果:

!image

示例 3:使用 #region 和 #endregion #region 将一组指令定义为一个代码块,编译器会一次性编译这个块。#endregion 标记该块的结束。下面的程序描述了这一点。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

    namespace preprocessor3
    {
        class Program
        {
            static void Main( string[] args )
            {
                char ch = ‘y‘;

                // Using #region to define a block of code
                #region
                if (ch == ‘y‘ || ch == ‘Y‘)
                    Console.WriteLine( "Value of ch is ‘y‘" );
                else
                    Console.WriteLine( "Value of ch is unknown" );

                // Ends the region
                #endregion
            }
        }
    }
    

输出:

Value of ch is ‘y
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/42811.html
点赞
0.00 平均评分 (0% 分数) - 0