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

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

เมธอด Type.GetTypeCode() ใน C# ใช้สำหรับดึงรหัสประเภท (TypeCode) ที่อยู่เบื้องล่างของ Type ที่ระบุ

ไวยากรณ์

ไวยากรณ์มีดังนี้:

public static TypeCode GetTypeCode (Type type);

พารามิเตอร์ type คือประเภทที่ต้องการดึงรหัสประเภท

ตัวอย่าง

ตัวอย่างการใช้งานเมธอด Type.GetTypeCode():

using System; 
public class Demo { 
    public static void Main(){ 
        Type type1 = typeof(double); 
        TypeCode type2 = Type.GetTypeCode(type1); 
        Console.WriteLine("TypeCode = " + type2); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

TypeCode = Double

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

ตัวอย่างที่สองกับประเภท short:

using System; 
public class Demo { 
    public static void Main(){ 
        Type type1 = typeof(short); 
        TypeCode type2 = Type.GetTypeCode(type1); 
        Console.WriteLine("TypeCode = " + type2); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

TypeCode = Int16