当前位置: 首页 > article >正文

【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”]]|hiddennumber是源代码的行号;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

http://www.kler.cn/a/445552.html

相关文章:

  • 【JAVA】JAVA泛型的<T>一时在前面一时在很后面怎么理解
  • 基于海思soc的智能产品开发(巧用mcu芯片)
  • Mybatis映射关系
  • 【C++】sophus : rxso3.hpp 实现了 3D 空间中的旋转和缩放操作的 RxSO3 类 (二十一)
  • 利用PHP和phpSpider进行图片爬取及下载
  • SpringBoot+Vue3实现阿里云视频点播 实现教育网站 在上面上传对应的视频,用户开会员以后才能查看视频
  • 【信息系统项目管理师】高分论文:论信息系统项目的进度管理(人力资源管理系统)
  • 基于Python3编写的Golang程序多平台交叉编译自动化脚本
  • AlipayHK支付宝HK接入-商户收款(PHP)
  • Java-29 深入浅出 Spring - IoC 基础 启动IoC容器的方式 Java方式与Web(XML、配置)方式
  • 游戏渠道假量解决方案
  • sql-labs 练习笔记
  • 二叉搜索树Ⅱ【东北大学oj数据结构8-2】C++
  • PDFMathTranslate - 基于AI的双语对照 PDF 翻译工具
  • Meta重磅发布Llama 3.3 70B:开源AI模型的新里程碑
  • 如何更改 maven 指定的 java 版本 set JAVA_HOME=C:\Program Files\Java\jdk1.8
  • Unity中对已经烘焙的物体进行复制却没有复制烘焙参数的处理
  • 【含开题报告+文档+PPT+源码】基于SpringBoot+Vue的校园勤工助学招聘系统的设计与实现
  • git暂存
  • 论文解读之Chain-of-Thought Prompting Elicits Reasoning in Large Language Models(CoT)