温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

深入浅析c#中的预处理指令

发布时间:2020-11-07 15:36:12 来源:亿速云 阅读:215 作者:Leah 栏目:开发技术

深入浅析c#中的预处理指令?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

预处理指令

这些指令/命令不会转换为可执行代码,但会影响编译过程的各个方面;列如,可以让编译器不编译某一部分代码等。

C#中主要的预处理指令

#define和#undef

#define指令定义:

#define DEBUG

它告诉编译器存在DEBUG这个符号;这个符号不是实际代码的一部分,而只是在编译器编译代码时候可能会根据这个符号做条件编译。

#undef定义:

#undef DEBUG

用来移除定义的符号DEBUG。如果不存在这样的标记,#undef指令则不会生效。同样,用#define再次定义一个同名的标记也不会有任何变化。

注意:

  • 你需要将#define和#undef指令写在实际业务代码开始之前的位置。
  • #define本身没有什么用,需要和其他预处理器指令结合使用;比如 #if
     

#if, #elif, #else和#endif

这些指令告诉编译器是否要编译包含在其中的代码块。例如:

int DoSomeWork(double x)
{
  // do something
  #if DEBUG
    Console.WriteLine($"x is {x}");
  #endif
}

这段代码中的Console.Writeline语句,只有在前面用#define指令定义了符号DEBUG后才会在编译的时候,真正被编译到;

如果编译器没发现DEBUG符号,就会在编译的时候忽略这句代码。 

#elif(= else if)和#else指令可以用在#if块中:

#define ENTERPRISE
#define W10
// further on in the file
#if ENTERPRISE
// do something
  #if W10
  // some code that is only relevant to enterprise
  // edition running on W10
  #endif
#elif PROFESSIONAL
// do something else
#else
// code for the leaner version
#endif

#if和#elif还支持有限的一些逻辑操作符,你可以用使用!,==,!=和||等。

一个标记如果存在,则认为是true,如果没有定义,就认为是false,因此你也可以这样使用:

	#if W10 && (ENTERPRISE==false) // if W10 is defined but ENTERPRISE isn't

#warning和#error

当编译器遇到#warning的时候,会产生警告信息;

当编译器遇到#error的时候,会产生错误信息;

  class Program
  {
    static void Main(string[] args)
    {
 
#warning this is a warning message which will be shown when compile
 
      Console.WriteLine("Hello World!");
 
#error this is a error message, and will break build
    }
  }

编译结果:

Program.cs(10,10): warning CS1030: #warning: 'this is a warning message which will be shown when compile' [/define_warning/define_warning.csproj]
Program.cs(14,8): error CS1029: #error: 'this is a error message, and will break build' [/define_warning/define_warning.csproj]
1 Warning(s)
1 Error(s)

  使用这些指令可以检查#define语句是不是做错了什么事,使用#warning可以提醒要做些事情:

#if DEBUG && RELEASE
#error "You've defined DEBUG and RELEASE simultaneously!"
#endif
#warning "Don't forget to remove this line before the boss tests the code!"
Console.WriteLine("*I love this job.*");

#region和#endregion

可以用来标识一段代码,在Visual Studio或其他能够识别的IDE里比较有用。

#region Member Field Declarations
int x;
double d;
Currency balance;
#endregion

#line

#line指令可以用来改变编译器输出警告和错误时相应的文件名和行号信息。这个实际中,用的可能会比较少。

主要是在用第三方包的时候,有时候会导致编译器报告的行号或文件名与实际不匹配。

#line可以用于还原这种匹配。

#line 164 "Core.cs" // We happen to know this is line 164 in the file Core.cs,
// before the intermediate package mangles it.
// later on
#line default // restores default line numbering

#pragma

#pragma指令可以用来终止或恢复某个指定编号到编译器警告。

与命令行选项不同,#pragma指令可以在类或方法级别实现。

例如:

class Program
{
  static void Main(string[] args)
  {
    int i = 0;
    Console.WriteLine("Hello World!");
  }
}

  编译是会有warning:

Program.cs(9,17): warning CS0219: The variable 'i' is assigned but its value is never used [/define_warning/define_warning.csproj]
1 Warning(s)
0 Error(s)  

从warning信息可以看出是warning CS0219,加入#pragma后就不会有warning了。

#pragma warning disable CS0219
  public class Program
  {
    static void Main(string[] args)
    {
      int i = 0;
      Console.WriteLine("Hello World!");
    }
  }
#pragma warning restore CS0219

注意:warning的代码是区分大小写的,CS2019要大写,如果写成cs2019则没有用。

看完上述内容,你们掌握深入浅析c#中的预处理指令的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI