{"id":1571,"date":"2023-02-06T11:56:34","date_gmt":"2023-02-06T03:56:34","guid":{"rendered":"http:\/\/zhang.mba\/?p=1571"},"modified":"2023-02-06T12:02:26","modified_gmt":"2023-02-06T04:02:26","slug":"czhong-deif","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/02\/06\/11\/56\/34\/1571\/czhong-deif\/c\/zhangzhiqi\/","title":{"rendered":"C#\u4e2d\u7684#if"},"content":{"rendered":"<p>\u5982\u679c C# \u7f16\u8bd1\u5668\u9047\u5230\u6700\u540e\u9762\u8ddf\u6709 #endif \u6307\u4ee4\u7684 #if \u6307\u4ee4\uff0c\u5219\u4ec5\u5f53\u6307\u5b9a\u7684\u7b26\u53f7\u5df2\u5b9a\u4e49\u65f6\uff0c\u5b83\u624d\u4f1a\u7f16\u8bd1\u8fd9\u4e24\u4e2a\u6307\u4ee4\u4e4b\u95f4\u7684\u4ee3\u7801\u3002 \u4e0e C \u548c C++ \u4e0d\u540c\uff0c\u60a8\u4e0d\u80fd\u5bf9\u7b26\u53f7\u8d4b\u4e88\u6570\u503c\uff1bC# \u4e2d\u7684 #if \u8bed\u53e5\u662f Boolean\uff0c\u4ec5\u6d4b\u8bd5\u7b26\u53f7\u662f\u5426\u5df2\u5b9a\u4e49\u3002 \u4f8b\u5982\uff0c<\/p>\n<pre><code class=\"language-C\uff03\">#define DEBUG\n\/\/ ...\n#if DEBUG\n    Console.WriteLine(&quot;Debug version&quot;);\n#endif\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd0\u7b97\u7b26 ==\uff08\u76f8\u7b49\uff09\u548c !=\uff08\u4e0d\u76f8\u7b49\uff09\u53ea\u80fd\u6d4b\u8bd5\u51fa\u7ed3\u679c\u4e3a true \u8fd8\u662f false\u3002 True \u8868\u793a\u7b26\u53f7\u5df2\u5b9a\u4e49\u3002 \u8bed\u53e5 #if DEBUG \u4e0e #if (DEBUG == true) \u7684\u542b\u4e49\u76f8\u540c\u3002 \u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26&amp;&amp; (and)\u3001|| (or) \u548c !(\u65e0) \u8ba1\u7b97\u591a\u4e2a\u7b26\u53f7\u662f\u5426\u5b9a\u4e49\u4e86\u3002 \u8fd8\u53ef\u4ee5\u7528\u62ec\u53f7\u5c06\u7b26\u53f7\u548c\u8fd0\u7b97\u7b26\u5206\u7ec4\u3002<\/p>\n<h2><a id=\"%E5%A4%87%E6%B3%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5907\u6ce8<\/h2>\n<p>\u7ed3\u5408\u4f7f\u7528 #if \u4e0e #else\u3001#elif\u3001#endif\u3001#define \u548c #undef \u6307\u4ee4\uff0c\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u7b26\u53f7\u662f\u5426\u5b58\u5728\u6765\u5305\u542b\u6216\u6392\u9664\u4ee3\u7801\u3002 \u5728\u7f16\u8bd1\u8c03\u8bd5\u7248\u672c\u7684\u4ee3\u7801\u6216\u9488\u5bf9\u7279\u5b9a\u914d\u7f6e\u8fdb\u884c\u7f16\u8bd1\u65f6\uff0c\u8fd9\u4f1a\u5f88\u6709\u7528\u3002<br \/>\n\u4ee5 #if \u6307\u4ee4\u5f00\u59cb\u7684\u6761\u4ef6\u6307\u4ee4\u5fc5\u987b\u7528 #endif \u6307\u4ee4\u663e\u5f0f\u7ec8\u6b62\u3002<br \/>\n#define \u4f7f\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7b26\u53f7\uff0c\u901a\u8fc7\u5c06\u8be5\u7b26\u53f7\u7528\u4f5c\u4f20\u9012\u7ed9 #if \u6307\u4ee4\u7684\u8868\u8fbe\u5f0f\uff0c\u4f7f\u8be5\u8868\u8fbe\u5f0f\u8ba1\u7b97\u4e3a true\u3002<br \/>\n\u4e5f\u53ef\u4ee5\u7528 \/define \u7f16\u8bd1\u5668\u9009\u9879\u6765\u5b9a\u4e49\u7b26\u53f7\u3002 \u53ef\u4ee5\u7528 #undef \u6765\u53d6\u6d88\u5b9a\u4e49\u7b26\u53f7\u3002<br \/>\n\u7528 \/define \u6216 #define \u5b9a\u4e49\u7684\u7b26\u53f7\u4e0e\u5177\u6709\u540c\u4e00\u540d\u79f0\u7684\u53d8\u91cf\u4e0d\u51b2\u7a81\u3002 \u5373\uff0c\u4e0d\u5e94\u5c06\u53d8\u91cf\u540d\u4f20\u9012\u5230\u9884\u5904\u7406\u5668\u6307\u4ee4\uff0c\u5e76\u4e14\u53ea\u80fd\u7528\u9884\u5904\u7406\u5668\u6307\u4ee4\u8ba1\u7b97\u7b26\u53f7\u3002<br \/>\n\u7528 #define \u521b\u5efa\u7684\u7b26\u53f7\u7684\u8303\u56f4\u662f\u5728\u5176\u4e2d\u5b9a\u4e49\u8be5\u7b26\u53f7\u7684\u6587\u4ef6\u3002<\/p>\n<h2><a id=\"%E7%A4%BA%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u793a\u4f8b<\/h2>\n<pre><code class=\"language-C\uff03\">\/\/ preprocessor_if.cs\n#define DEBUG\u00a0\n#define MYTEST\nusing System;\npublic class MyClass \n{\n    static void Main() \n    {\n#if (DEBUG &amp;&amp; !MYTEST)\n        Console.WriteLine(&quot;DEBUG is defined&quot;);\n#elif (!DEBUG &amp;&amp; MYTEST)\n        Console.WriteLine(&quot;MYTEST is defined&quot;);\n#elif (DEBUG &amp;&amp; MYTEST)\n        Console.WriteLine(&quot;DEBUG and MYTEST are defined&quot;);\n#else\n        Console.WriteLine(&quot;DEBUG and MYTEST are not defined&quot;);\n#endif\n    }\n}\n<\/code><\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c C# \u7f16\u8bd1\u5668\u9047\u5230\u6700\u540e\u9762\u8ddf\u6709 #endif \u6307\u4ee4\u7684 #if \u6307\u4ee4\uff0c\u5219\u4ec5\u5f53\u6307\u5b9a\u7684\u7b26\u53f7\u5df2\u5b9a\u4e49\u65f6\uff0c\u5b83\u624d\u4f1a\u7f16\u8bd1\u8fd9\u4e24\u4e2a\u6307\u4ee4\u4e4b\u95f4\u7684\u4ee3\u7801\u3002 \u4e0e C \u548c C++ \u4e0d\u540c\uff0c\u60a8\u4e0d\u80fd\u5bf9\u7b26\u53f7\u8d4b\u4e88\u6570\u503c\uff1bC# \u4e2d\u7684 #if \u8bed\u53e5\u662f Boolean\uff0c\u4ec5\u6d4b\u8bd5\u7b26\u53f7\u662f\u5426\u5df2\u5b9a\u4e49\u3002 \u4f8b\u5982\uff0c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[46],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1571"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1571"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1571\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}