以下代码特别简单,但是如果没用过的话,可能直接看代码也是看不懂用途。所以这里还是大致表述下
- 没有自己定义过的DEBUG和TRACE为何能直接使用?——右键项目,可以看到定义DEBUG常量和定义TRACE常量,前面打勾即可
- 如何确定DEBUG模式?——直接切换生成模式既可以看到效果
- warning和error指令的体现?——基于现在vs的强大,所以我们只要写出来就会直接体现出来警告或者错误,如果和生成模式结合使用未直接体现,尝试重新生成解决方案
- line指令——断点调试跟踪即可看到效果
- define和undef——自定义模式的装载和卸载
- region和endregion代码折叠——这个大家比较常用,就没写了
- 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");
}
}
}
由简入繁,拿来即用
更多精彩,请关注微信公众号: