เมธอด Type.GetElementType() ใน C# ใช้เพื่อส่งคืนชนิดของอ็อบเจ็กต์ที่ถูกล้อมหรืออ้างอิงถึงโดยอาร์เรย์ปัจจุบัน ตัวชี้ หรือประเภทอ้างอิง
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public abstract Type GetElementType ();
ตัวอย่าง
ให้เราดูตัวอย่างการใช้เมธอด Type.GetElementType() -
using System;
public class Demo {
public static void Main(){
string[] arr = {"tom", "amit", "kevin", "katie"};
Type t1 = arr.GetType();
Type t2 = t1.GetElementType();
Console.WriteLine("Type = "+t2.ToString());
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Type = System.String
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อนำวิธีการ Type.GetElementType() ไปใช้ -
using System;
public class Demo {
public static void Main(){
Type t1 = typeof(int[,,,, ]);
Type t2 = t1.GetElementType();
Console.WriteLine("Type = "+t2.ToString());
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Type = System.Int32