加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 请定义#define

发布时间:2020-12-15 17:13:30 所属栏目:百科 来源:网络整理
导读:有人可以给我一些如何在c#中使用#define的例子吗? #define //preprocessor directive 它的目的是什么?这是微软的例子,我仍然没有得到: // preprocessor_if.cs#define DEBUG#define MYTESTusing System;public class MyClass { static void Main() {#if (D
有人可以给我一些如何在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的结转.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读