Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Chỉ thị #define tiền xử lý trong C#

#define định nghĩa một chuỗi ký tự gọi là symbol. Tạo hằng số ký hiệu.

#define cho phép định nghĩa symbol sao cho khi dùng làm biểu thức trong #if, biểu thức evaluate thành true.

Ví dụ

#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
        }
    }
}