เมธอด GetType() ของคลาสอาร์เรย์ใน C# รับประเภทของอินสแตนซ์ปัจจุบัน (สืบทอดมาจาก Object)
เพื่อให้ได้แบบ
Type tp = value.GetType();
ในตัวอย่างด้านล่าง เรากำลังตรวจสอบค่า int โดยใช้ประเภท
if (tp.Equals(typeof(int)))
Console.WriteLine("{0} is an integer data type.", value) ต่อไปนี้เป็นการใช้งาน GetType() วิธีการใน C#.
ตัวอย่าง
using System
public class Program {
public static void Main() {
object[] values = { (int) 100, (long) 17111};
foreach (var value in values) {
Type tp = value.GetType();
if (tp.Equals(typeof(int)))
Console.WriteLine("{0} is aninteger data type.", value);
else
Console.WriteLine("'{0}' is not an int data type.", value);
}
}
}