分享

C# 预处理器指令

 Csharp小记 2023-10-29 发布于江苏

     这篇文章我非常倾向于用视频表现出来,这样可以更直观的感受到各个指令的使用方式,无奈我表述能力实在是欠缺,所以还是直接放代码出来吧;

以下代码特别简单,但是如果大家没用过的话,可能直接看代码也是看不懂用途。所以我这里还是大致表述下:

  1. 没有自己定义过的DEBUG和TRACE为何能直接使用?——右键项目,可以看到定义DEBUG常量和定义TRACE常量,前面打勾即可

  2. 如何确定DEBUG模式?——直接切换生成模式既可以看到效果

  1. warning和error指令的体现?——基于现在vs的强大,所以我们只要写出来就会直接体现出来警告或者错误,如果和生成模式结合使用未直接体现,尝试重新生成解决方案

  2. line指令——断点调试跟踪即可看到效果

  1. define和undef——自定义模式的装载和卸载

  2. region和endregion代码折叠——这个大家比较常用,就没写了

  1. pragma指令——我感觉不会用到~~~

实现代码:

/* * #if * #else * #elif * #endif * #define * #undef * #warning * #error * #line * #region * #endregion * #pragma * #pragma warning * #pragma checksum */
//#define DEBUG//#undef DEBUG
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace Preprocessor{ class Program { static void Main(string[] args) {#if DEBUG#warning DEBUG模式 Console.WriteLine(0); Console.WriteLine(1);#elif TRACE Console.WriteLine("TRACE");
#else#error 非DEBUG模式#endif int x = 0; x++; x++;#line 44 Console.WriteLine(x); Console.WriteLine(2); test1(); Console.ReadKey(); }
[System.Diagnostics.Conditional("DEBUG")] static void test1() { Console.WriteLine("test1"); } }}

由简入繁,拿来即用

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多