เมธอด UInt16.GetTypeCode() ใน C# คืนค่า TypeCode สำหรับประเภทค่า UInt16
ไวยากรณ์
public TypeCode GetTypeCode();
ตัวอย่างที่ 1
using System;
public class Demo
{
public static void Main()
{
ushort val1 = 55;
ushort val2 = 100;
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
Console.WriteLine("Typecode for val1 = " + type1);
Console.WriteLine("Typecode for val2 = " + type2);
}
}
ผลลัพธ์
Typecode for val1 = UInt16
Typecode for val2 = UInt16
ตัวอย่างที่ 2: กับ MinValue และ 0
using System;
public class Demo
{
public static void Main()
{
ushort val1 = UInt16.MinValue;
ushort val2 = 0;
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
Console.WriteLine("Typecode for val1 = " + type1);
Console.WriteLine("Typecode for val2 = " + type2);
}
}
ผลลัพธ์
Typecode for val1 = UInt16
Typecode for val2 = UInt16
ประโยชน์: ใช้ตรวจสอบประเภทข้อมูลใน Runtime หรือเมื่อทำงานกับ Reflection