Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Trường UInt32.MaxValue trong C# với các ví dụ

Trường UInt32.MaxValue trong C# biểu thị giá trị lớn nhất của kiểu số nguyên không dấu 32-bit.

Cú pháp

Cú pháp như sau:

public const uint MaxValue = 4294967295;

Ví dụ 1

Ví dụ minh họa sử dụng trường UInt32.MaxValue:

using System;
public class Demo
{
    public static void Main()
    {
        uint val1 = 23;
        uint val2 = 0;
        Console.WriteLine("Value1 = " + val1);
        Console.WriteLine("Value2 = " + val2);
        Console.WriteLine("HashCode for value1 = " + val1.GetHashCode());
        Console.WriteLine("HashCode for value2 = " + val2.GetHashCode());
        Console.WriteLine("Are they equal? = " + (val1.Equals(val2)));
        TypeCode type1 = val1.GetTypeCode();
        TypeCode type2 = val2.GetTypeCode();
        Console.WriteLine("TypeCode for val1 = " + type1);
        Console.WriteLine("TypeCode for val2 = " + type2);
        Console.WriteLine("Maximum Value = " + UInt32.MaxValue);
    }
}

Đầu ra

Kết quả chương trình:

Value1 = 23
Value2 = 0
HashCode for value1 = 23
HashCode for value2 = 0
Are they equal? = False
TypeCode for val1 = UInt32
TypeCode for val2 = UInt32
Maximum Value = 4294967295

Ví dụ 2

Ví dụ kiểm tra phạm vi chuyển đổi từ ulong sang uint:

using System;
public class Demo
{
    public static void Main()
    {
        ulong[] val = { 568787, 8343443, 243434343 };
        uint res;
        foreach (ulong a in val)
        {
            if (a >= UInt32.MinValue && a <= UInt32.MaxValue)
            {
                res = Convert.ToUInt32(a);
                Console.WriteLine("Converted: " + res);
            }
            else
            {
                Console.WriteLine("Not Possible");
            }
        }
    }
}

Đầu ra

Kết quả chạy chương trình:

Converted: 568787
Converted: 8343443
Converted: 243434343