1.宏-釋義
在C#中,宏(Macro)通常指的是預處理指令(Preprocessor Directive),用于在編譯時對源代碼進行一些宏替換或條件編譯的操作。C#中的宏使用預處理器指令#define
和#undef
來定義和取消定義宏,并使用#if
、#elif
、#else
和#endif
等指令來進行條件編譯。
2.代碼示例
#define DEBUG//1.定義宏
//#undef DEBUG//取消定義宏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Program{static void Main(string[] args){
#if DEBUGConsole.WriteLine("Debug模式");
#elseConsole.WriteLine("Release模式");
#endifConsole.WriteLine("這是一條普通的輸出語句");//#if CUSTOM_DEBUG// Console.WriteLine("這是自定義調試模式下的輸出語句");//#endif// Console.ReadLine();Console.ReadLine();}}
}