Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Type.GetElementType() ใน C#

เมธอด Type.GetElementType() ใน C# ใช้คืนค่า Type ของออบเจ็กต์ที่ถูกรวมหรืออ้างอิงโดยประเภทอาร์เรย์ ตัวชี้ (pointer) หรือประเภทอ้างอิง (reference type) ปัจจุบัน

ไวยากรณ์

public abstract Type GetElementType ();

ตัวอย่าง

ตัวอย่างที่ 1: ดึงชนิดข้อมูลของอาร์เรย์สตริง

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

ตัวอย่างเพิ่มเติม

ตัวอย่างที่ 2: ดึงชนิดข้อมูลของอาร์เรย์หลายมิติ int[,,,,]

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