โอเปอเรเตอร์ "is" ใน C# จะตรวจสอบว่าประเภทรันไทม์ของออบเจ็กต์เข้ากันได้กับประเภทที่กำหนดหรือไม่
ต่อไปนี้เป็นรูปแบบไวยากรณ์
expr is type
ที่นี่ ด่วน คือนิพจน์
ประเภท เป็นชื่อประเภท
ต่อไปนี้คือตัวอย่างการแสดงการใช้ is operator ใน C#
ตัวอย่าง
using System;
class One {
}
class Two {
}
public class Demo {
public static void Test(object obj) {
One x;
Two y;
if (obj is One) {
Console.WriteLine("Class One");
x = (One)obj;
} else if (obj is Two {
Console.WriteLine("Class Two");
y = (Two)obj;
} else {
Console.WriteLine("None of the classes!");
}
}
public static void Main() {
One o1 = new One();
Two t1 = new Two();
Test(o1);
Test(t1);
Test("str");
Console.ReadKey();
}
}