【C#】预处理指令

预处理指令

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

csharp 复制代码
#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 必须放到第一行

csharp 复制代码
#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相关,表示一个文件里面,可以拆分为不同的文件,并且下面代码是在第几行

csharp 复制代码
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

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

综合

csharp 复制代码
#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#上位机通信故障排查步骤手l
网络·c#
小李独爱秋6 分钟前
计算机网络经典问题透视:媒体播放器与媒体服务器的AB面
运维·服务器·网络协议·计算机网络·媒体
野犬寒鸦8 分钟前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
java·服务器·数据库·分布式·后端·rabbitmq
想唱rap9 分钟前
MySQL表得内外连接
服务器·数据库·c++·mysql·ubuntu
Lam㊣9 分钟前
CentOS上搭建时间同步服务器
linux·服务器·centos
北海速度网络10 分钟前
广东IP持续刷量攻击难根治?深度剖析PCDN流量劫持与JA3/JA4精准防护方案
服务器·网络·tcp/ip
豆豆10 分钟前
如何在liunx环境安装PageAdmin Cms系统
linux·服务器·云计算·cms·建站系统·建站平台·网站管理系统
熏鱼的小迷弟Liu12 分钟前
【Redis】Redis为什么快?
数据库·redis·缓存
Gauss松鼠会13 分钟前
【opengauss】opengauss使用gs_probackup进行增量备份恢复
数据库·sql·database·opengauss
Gauss松鼠会13 分钟前
【GaussDB 】GaussDB 性能排查与锁等待分析常用 SQL 全面解析(TOP SQL / 等锁 / 长事务 / LwLock / SubPlan)
数据库·sql·gaussdb