有人可以给我一些如何在c#中使用#define的例子吗?
#define //preprocessor directive
它的目的是什么?这是微软的例子,我仍然没有得到:
// preprocessor_if.cs #define DEBUG #define MYTEST using System; public class MyClass { static void Main() { #if (DEBUG && !MYTEST) Console.WriteLine("DEBUG is defined"); #elif (!DEBUG && MYTEST) Console.WriteLine("MYTEST is defined"); #elif (DEBUG && MYTEST) Console.WriteLine("DEBUG and MYTEST are defined"); #else Console.WriteLine("DEBUG and MYTEST are not defined"); #endif } }
解决方法
Quote:
The #define and #undef lines should appear at the very top of a source
text file and they can adjust compilation options for the entire file.
和
In the C# language,the #define line is considered a preprocessing
directive. There are some invalid Syntaxes for defined symbols; you
cannot use a number value as the defined identifier,for example.
和
The #undef directive ensures that after the textual point in the file,
the specified identifier is not defined.
在您的示例中,如果定义了DEBUG(只要定义了它并不重要)并且未定义MYTEST,那么它将显示所显示的消息.
否则,如果未定义DEBUG但定义了MYTEST,则它将显示所显示的消息.
底线:
定义的要点是有选择地将编译选项应用于程序,以便为其提供不同的程序流程.它是C和C的结转.