c# – 请定义#define

前端之家收集整理的这篇文章主要介绍了c# – 请定义#define前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有人可以给我一些如何在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的结转.

原文链接:https://www.f2er.com/csharp/100462.html

猜你在找的C#相关文章