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

การดึง TypeCode ของชนิดค่า UInt32 ใน C#

ภาพรวม

โครงสร้าง UInt32 (unsigned 32-bit integer) มีเมธอด GetTypeCode() คืน TypeCode.UInt32

ตัวอย่างที่ 1: ค่า 55 และ 100

using System;

public class Demo
{
    public static void Main()
    {
        uint val1 = 55;
        uint 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 = UInt32
Typecode for val2 = UInt32

ตัวอย่างที่ 2: UInt32.MinValue และ 0

using System;

public class Demo
{
    public static void Main()
    {
        uint val1 = UInt32.MinValue; // 0
        uint 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 = UInt32
Typecode for val2 = UInt32

สรุป

  • GetTypeCode() คืน enum TypeCode ระบุชนิดมาตรฐาน
  • UInt32 ใช้ 4 ไบต์ ช่วง 0 ถึง 4,294,967,295
  • ใช้สำหรับการ serialize, interop, หรือตรวจสอบชนิดข้อมูล runtime