使用条件属性 [Conditional("DEBUG")],而不使用#if DEBUG

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 using System;
2 using System.Diagnostics;
3 namespace TraceFunctions
4 {
5 public class Trace
6 {
7 [Conditional( " DEBUG " )] // 条件属性
8 public static void Message( string traceMessage)
9 {
10 Console.WriteLine( " [TRACE] - " + traceMessage);
11 }
12 }
13 }
14
15 using System;
16 using TraceFunctions;
17
18 public class TraceClient
19 {
20 public static void Main( string [] args)
21 {
22 Trace.Message( " Main Starting " );
23
24 if (args.Length == 0 )
25 {
26 Console.WriteLine( " No arguments have been passed " );
27 }
28 else
29 {
30 for ( int i = 0 ; i < args.Length; i ++ )
31 {
32 Console.WriteLine( " Arg[{0}] is [{1}] " ,i,args[i]);
33 }
34 }
35
36 Trace.Message( " Main Ending " );
37 }
38 }

如果使用#if DEBUG,则为

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 using System;
2 using System.Diagnostics;
3 namespace TraceFunctions
4 {
5 public class Trace
6 {
7 public static void Message( string traceMessage)
8 {
9 #if DEBUG
10 Console.WriteLine( " [TRACE] - " + traceMessage);
11 #endif
12 }
13 }
14 }
15 using System;
16 using TraceFunctions;
17
18 public class TraceClient
19 {
20 public static void Main( string [] args)
21 {
22 Trace.Message( " Main Starting " );
23
24 if (args.Length == 0 )
25 {
26 Console.WriteLine( " No arguments have been passed " );
27 }
28 else
29 {
30 for ( int i = 0 ; i < args.Length; i ++ )
31 {
32 Console.WriteLine( " Arg[{0}] is [{1}] " ,i,args[i]);
33 }
34 }
35
36 Trace.Message( " Main Ending " );
37 }
38 }
在DEGUB下和之前的是一样的,但在Release下, public static void Message(string traceMessage)函数为空函数,此时它什么也不做,但却会被调用,增大了开销

转载于:https://www.cnblogs.com/21vicky21/archive/2011/04/07/2007481.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值