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

ฟิลด์ Int64.MaxValue ใน C# พร้อมตัวอย่าง


ฟิลด์ UInt64.MaxValue ใน C# แทนค่าสูงสุดของจำนวนเต็ม 64 บิตไม่มีเครื่องหมาย

ไวยากรณ์

public const ulong MaxValue = 18446744073709551615;

ตัวอย่าง

using System; 
public class Demo { 
    public static void Main(){ 
        ulong val1 = 879879879; 
        ulong val2 = 464556565; 
        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 = " + UInt64.MaxValue); 
    } 
}

ผลลัพธ์

Value1 = 879879879 
Value2 = 464556565 
HashCode for value1 = 879879879 
HashCode for value2 = 464556565 
Are they equal? = False 
TypeCode for val1 = UInt64 
TypeCode for val2 = UInt64 
Maximum Value = 18446744073709551615

ตัวอย่างที่ 2 (ตรวจสอบช่วงค่า)

using System; 
public class Demo { 
    public static void Main(){ 
        ulong[] val = {8998, 899898778, 878787878}; 
        ulong res; 
        foreach(ulong a in val){ 
            if(a >= UInt64.MinValue && a <= UInt64.MaxValue){ 
                res = Convert.ToUInt64(a); 
                Console.WriteLine("Converted: " + res); 
            } else { 
                Console.WriteLine("Not Possible"); 
            } 
        } 
    } 
}

ผลลัพธ์

Converted: 8998 
Converted: 899898778 
Converted: 878787878