前两天刚看书看到的,来这里跟大家分享一下有关C#处理指令的问题

前两天刚看书看到的,来这里跟大家分享一下,

这些东东就是C# 预处理器的指令了,在我们编译的时候会很有用处.
在处理之前要先定义的一个符号,可以在后面根据条件进行编译,
#define
这便是定义指令.必须在文件的开始处定义才行,不然是产生编译错误
如下定义

 #define first
 
#define Debug
 
using System;

在定义以后我们就要来进行条件设置了,不然就达不到效果,呵呵,
注意: #define 定义的符号在后面的条件计算是为 true
如:

 #define first
 
#define Debug
 
using System;

namespace Control1
{
 
class MainClass
 
{
  
public static void Main(string[] args)
  
{
   
#if first
   Console.WriteLine(
"Hello World first!");
   
#else
   Console.WriteLine(
"Helleo World other!");
   
#endif
  }

 }

}


这样的结果是:Hello World first!在这里我们用到了#else这个跟条件语句里的else上一样的.
因为在开始的时候我们定义了first这个符号.从上面可以看出first是作为布尔变量来计算的.
现在我们将这段代码变换一下

 #define first
 
#define Debug

using System;

namespace Control1
{
    
class MainClass
    
{
        
public static void Main(string[] args)
        
{
            
#if (first && Debug)
            Console.WriteLine(
"Hello World Yes all!");
            
#elif (!first && Debug)
            Console.WriteLine(
"Helleo World no first!");
            
#elif (first && !Debug)
            Console.WriteLine(
"Helleo World no Debug!");
            
#else
            Console.WriteLine(
"Helleo World no all!");
            
#endif
        }

    }

}


这里看到的效果是:Hello World Yes all!
上面用到了#elif这相当于条件语句里的 else if
接下来我们看看#undef

 #define first
 
#define Debug
 
#undef Debug
using System;

namespace Control1
{
    
class MainClass
    
{
        
public static void Main(string[] args)
        
{
            
#if (first && Debug)
            Console.WriteLine(
"Hello World Yes all!");
            
#else
            Console.WriteLine(
"Hello World No all!");
            
#endif
        }

        
    }

}


现在看到的结果是:Hello World No all!
为什么,呵呵.是因为我们写了#undef取消了对Debug的定义,也就是说Debug已经不起作用了,
接下来该#warning上场了,故名思义,就是在编译的时候产生警告信息.

 #define first
 
#define Debug
 
#undef Debug
using System;

namespace Control1
{
    
class MainClass
    
{
        
public static void Main(string[] args)
        
{
            
#if first
            Console.WriteLine(
"Hello Word!");
            
#warning Hello first
            
#endif
        }

        
    }

}

 

可以看到有Hello Word!
在下面的信息框里可以看到警告信息,编译是已经通过了,
下面继续看其它的指令:#error这是一个产生错误信息的指令如下
 #define first
 
#define Debug
 
#undef Debug
using System;

namespace Control1
{
    
class MainClass
    
{
        
public static void Main(string[] args)
        
{
            
#if first
            Console.WriteLine(
"Hello Word!");
            
#error Hello first
            
#endif
        }

        
    }

}


这里就看不到输入的字符了,不过可以在消息框里看到下面的信息


现在来看看#line指令

using System;

namespace Control1
{
 
class MainClass
 
{
  
public static void Main(string[] args)
  
{
    
#line 200
        
int i;
        
#line default
       
char c;

  }

  
 }

}


这样可以看到消息框有台下信息

#line的功能就显而易见了,他就是将默认的行号改成自己定义的行号.#line后面跟default则是将现在的行号恢复至原来的行号
但是它还有一个功能就是可以隐藏某行代码,这是SDK里对它的解释

下面的示例说明调试器如何忽略代码中的隐藏行。运行此示例时,它将显示三行文本。但是,当设置如示例所示的断点并按 F10 键逐句通过代码时,您将看到调试器忽略了隐藏行。另请注意,即使在隐藏行上设置断点,调试器仍会忽略它。]

      Console.WriteLine("Normal line #1.");   // Set a break point here.
      #line hidden
      Console.WriteLine(
"Hidden line.");
      
#line default
      Console.WriteLine(
"Normal line #2.");


这段代码在运行后虽然可以看到有三行输入,但是在调试的时候可以发现第二行根本就没有被调试到.
还有最后两个指令了,
#region
#endregion
这两个我想大家都非常熟悉了吧

现在看到的结果是:Hello World No all! 为什么,呵呵.是因为我们写了#undef取消了对Debug的定义,也就是说Debug已经不起作用了, 接下来该#warning上场了,故名思义,就是在编译的时候产生警告信息.   可以看到有Hello Word! 在下面的信息框里可以看到警告信息,编译是已经通过了, 下面继续看其它的指令:#error这是一个产生错误信息的指令如下

现在看到的结果是:Hello World No all! 为什么,呵呵.是因为我们写了#undef取消了对Debug的定义,也就是说Debug已经不起作用了, 接下来该#warning上场了,故名思义,就是在编译的时候产生警告信息.   可以看到有Hello Word! 在下面的信息框里可以看到警告信息,编译是已经通过了, 下面继续看其它的指令:#error这是一个产生错误信息的指令如下

posted @ 2005-01-21 20:13  skylai  阅读(677)  评论(0编辑  收藏  举报