C# 预处理指令

简介: C# 预处理指令

预处理指令

它提供了一种控制某一块代码编译与否的方法。

#define DEBUG
class MyClass
{
   
    int x;
    void Foo()
    {
   
        #if DEBUG
        Console.WriteLine("Testing:x={0}",x);
        #endif
    }
}

常用指令

预处理指令 含义
#define symbol 定义symbol,必须放到文件的第一行
#undef symbol 取消symbol 定义
#if symbol [operator symbol] 判断symbol符号。其中operator 可以是==、!=、&&、\ \ 。#if指令后可以跟#else、#elif和#endif
#else 加在#if后面
#elif symbol [operator symbol2] zudhe #else 和 #if判断
#endif 结束条件指令
#warning text 在编译器输出中显示text警告信息
#error text 在编译器输出中显示text错误信息
#pragma warning [disable \ restore] 禁用/恢复编译器警告
#line [number["file"]]\ hidden number是源代码的行号;file是输出的文件名;hidden指示调试器忽略此处到下一个#line指令之间的代码
#region name 标记大纲的开始位置
#endregion 结束一个大纲区域

案例:

if 、#else 、#elif相关

define symbol 必须放到第一行

#define TOM
#define NET60

namespace TopSet17
{
   
    internal class Program
    {
   
        static void Main(string[] args)
        {
   

#if TOM
            Console.WriteLine("Debug mode is on.");
#elif TOM01
            Console.WriteLine("tom01");
#endif

            #region 正文
            Console.WriteLine("Hello, World!");
            #endregion

#if NET40
    // Code specific to .NET 4.0
    Console.WriteLine("This code will only compile for .NET 4.0.");

#elif NET45
    // Code specific to .NET 4.5
    Console.WriteLine("This code will only compile for .NET 4.5.");
#elif NET60
    // Code specific to .NET 6.0
    Console.WriteLine("This code will only compile for .NET 6.0.");
#else
            // Code specific to .NET 4.5
            Console.WriteLine("This code will only compile for .NET other");
#endif

        }
    }
}

//输出
Debug mode is on.
Hello, World!
This code will only compile for .NET 6.0.

line相关,表示一个文件里面,可以拆分为不同的文件,并且下面代码是在第几行

static void Main(string[] args)
{
   
    Method1();
}
#line 1 "Program1.cs"
    public static void Method1()
{
   
    Console.WriteLine("Method1 is called.");
}

#line 1 "Program2.cs"
    public static void Method2()
{
   
    Console.WriteLine("Method2 is called.");
}

error 、 #warn

#define TOM1
static void Main(string[] args)
{
   
    #if TOM
    //如果定义了TOM,那么下面就会在编译器报错
    #error "my error"
    #endif
    #if TOM1
    #warning "my warning"
    #endif
}

综合

#define Debug//调试
#define Release//发布
#undef Debug//取消定义调试

#line 100
#pragma warning disable CS0219//消除CS0219警告
#region "代码折叠器"
#if Debug&&Release==false
    //调试版本执行
#elif !Debug&&Release
    //发布版本执行
#elif !Debug&&!Release
    //两个版本都不存在
    #error "两个版本都不存在";
#elif
    //发布和调试版本都执行
#endif
#warning "最后不要忘了把这段语句去掉";
#endregion
目录
相关文章
|
5月前
|
编译器 C# 开发者
C# 10.0中的全局`using`指令:简化命名空间引用的新方式
【1月更文挑战第4天】本文介绍了C# 10.0中引入的全局`using`指令,该指令允许开发者在项目级别统一管理命名空间引用,从而消除源文件中重复的`using`语句。全局`using`指令通过减少冗余代码、提高可维护性和统一命名空间管理,为开发者带来了更高效的编码体验。文章详细解释了如何实现全局`using`指令,并探讨了其在实际项目中的优势和适用场景。
|
4月前
|
编译器 C# C++
【.NET Core】C#预处理器指令
【.NET Core】C#预处理器指令
51 1
|
4月前
|
C#
C# 命名空间和 using 指令详解
C# 命名空间和 using 指令详解
|
编译器 C# C++
C#——预处理器指令
C#——预处理器指令
117 0
|
编译器 C#
C#编程-82:编译预处理_
C#编程-82:编译预处理_
111 0
C#编程-82:编译预处理_
|
编译器 C# C++
☀️ 学会编程入门必备 C# 最基础知识介绍(六)——接口、命名空间、预处理指令、正则表达式、异常处理、文件的输入与输出
前言👻 接口(interface)🎉 定义接口: MyInterface.cs 接口继承: InterfaceInheritance.cs C# 命名空间(Namespace)🎈 定义命名空间 using 关键字 嵌套命名空间 C# 预处理器指令🎄 C# 预处理器指令列表 #define 预处理器 条件指令 C# 正则表达式🔔 定义正则表达式 C# 异常处理🎃 语法 C# 中的异常类 异常处理 创建用户自定义异常 抛出对象 C# 文件的输入与输出⛄️ C# I/O 类 FileStream 类 C# 高级文件操作
☀️ 学会编程入门必备 C# 最基础知识介绍(六)——接口、命名空间、预处理指令、正则表达式、异常处理、文件的输入与输出
|
编译器 C#
C#编程-82:编译预处理
C#编程-82:编译预处理
101 0
C#编程-82:编译预处理
|
Java C#
C# 证明指令重排
C# 证明指令重排
4524 0
|
9天前
|
安全 C# 数据安全/隐私保护
实现C#编程文件夹加锁保护
【10月更文挑战第16天】本文介绍了两种用 C# 实现文件夹保护的方法:一是通过设置文件系统权限,阻止普通用户访问;二是使用加密技术,对文件夹中的文件进行加密,防止未授权访问。提供了示例代码和使用方法,适用于不同安全需求的场景。