#error Directive
สร้าง Compile-time Error ขึ้นเอง หยุดการ Build ทันที
#if (!DEBUG) #error "This code requires DEBUG mode!" #endif
#warning Directive
สร้าง Compile-time Warning แสดงเตือน แต่ Build ผ่านได้
#if RELEASE #warning "Performance optimizations disabled in RELEASE build" #endif
ตัวอย่าง #error
using System;
namespace Demo
{
class Program
{
public static void Main(string[] args)
{
#if (!ONE)
#error ONE is undefined
#endif
Console.WriteLine("Generating a user-defined error!");
}
}
}
Output (Build Error)
Compilation failed: 1 error(s), 0 warnings error CS1029: #error: 'ONE is undefined'
ตัวอย่าง #warning
using System;
namespace Demo
{
class Program
{
public static void Main(string[] args)
{
#if (!TWO)
#warning TWO is undefined
#endif
Console.WriteLine("Generates a warning!");
}
}
}
Output (Build Warning)
warning CS1030: #warning: 'TWO is undefined' Generates a warning!
กรณีใช้งานจริง
#error: บังคับ Configuration, ตรวจสอบ API Version, Deprecation Notice#warning: TODO/FIXME Reminder, Obsolete API Usage, Experimental Features