Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

#define Pre-processor Directive ใน C#

#define กำหนด Symbol (ลำดับตัวอักษร) สร้าง Symbolic Constants

#define ทำให้ Symbol นั้น Evaluate เป็น True เมื่อใช้กับ #if

ตัวอย่าง

#define ONE
#undef TWO
using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            #if (ONE && TWO)
            Console.WriteLine("Both are defined");
            #elif (ONE && !TWO)
            Console.WriteLine("ONE is defined and TWO is undefined");
            #elif (!ONE && TWO)
            Console.WriteLine("ONE is defined and TWO is undefined");
            #else
            Console.WriteLine("Both are undefined");
            #endif
        }
    }
}