#คำสั่งผิดพลาด
คำสั่ง #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!");
}
}
} หลังจากรันโปรแกรมข้างต้น จะเกิดข้อผิดพลาดที่ผู้ใช้กำหนดขึ้น -
ผลลัพธ์
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!");
}
}
} หลังจากรันโปรแกรมข้างต้นแล้ว คำเตือนจะถูกสร้างขึ้นและสามารถมองเห็นผลลัพธ์ได้ -
ผลลัพธ์
warning CS1030: #warning: `TWO is undefined' Generates a warning!