C#预处理指令

  C#预处理指令,几乎每本讲到基础知识的书都会讲到这个,一直也是看的不求甚解,经常用的的就是#region name #endregion 来对代码做一个整体的功能注释而已。

  C#预处理指令,可以帮助我们来将编译出来的代码版本进行分割,类似Debug和Release的区别。

  我们可以#define 1 #define 2  分别代表版本1和版本2,这样我们只需要通过#if 2就可以编译出只有第2个版本才有的功能了。

 

  详细的内容请看易百教程的http://www.yiibai.com/csharp/csharp_preprocessor_directives.html

  或查看 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/preprocessor-directives/index

  我在这里做做补充就好,易百上讲的#define指令可在一个类的上头定义如:

 1 #define PI 
 2 using System;
 3 namespace PreprocessorDAppl
 4 {
 5    class Program
 6    {
 7       static void Main(string[] args)
 8       {
 9          #if (PI)
10             Console.WriteLine("PI is defined");
11          #else
12             Console.WriteLine("PI is not defined");
13          #endif
14          Console.ReadKey();
15       }
16    }
17 }

  那么,我换个类来定义是否依然能够找到这个 #define PI 吗?答案是不可以,那么我们通过什么来设置一个全局的 #define PI ?   如图所示

  

 

  

   将#define PI放在代码中会提示以下错误

  

 

posted @ 2017-09-06 11:37  风中寻觅  阅读(245)  评论(0)    收藏  举报