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

Phương thức UInt64.ToString() trong C# với các ví dụ


Phương thức UInt64.ToString() trong C# được sử dụng để chuyển đổi giá trị số của thể hiện UInt64 hiện tại thành biểu diễn chuỗi tương đương.

Cú pháp

Dưới đây là cú pháp:

public override string ToString();

Ví dụ 1

Hãy xem ví dụ sau để triển khai phương thức UInt64.ToString():

using System;
public class Demo {
    public static void Main(){
        ulong val1 = 465656665;
        ulong val2 = 232525678;
        Console.WriteLine("Value1 (String representation) = " + val1.ToString());
        Console.WriteLine("Value2 (String representation) = " + val2.ToString());
        bool res = val1.Equals(val2);
        Console.WriteLine("Return value (comparison) = " + res);
        if (res)
            Console.WriteLine("val1 = val2");
        else
            Console.WriteLine("val1 != val2");
    }
}

Đầu ra

Chương trình sẽ cho ra kết quả sau:

Value1 (String representation) = 465656665
Value2 (String representation) = 232525678
Return value (comparison) = False
val1 != val2

Ví dụ 2

Ví dụ khác triển khai phương thức UInt64.ToString() với giá trị 0 và MaxValue:

using System;
public class Demo {
    public static void Main(){
        ulong val1 = 0;
        ulong val2 = UInt64.MaxValue;
        Console.WriteLine("Value1 (String representation) = " + val1.ToString());
        Console.WriteLine("Value2 (String representation) = " + val2.ToString());
        bool res = val1.Equals(val2);
        Console.WriteLine("Return value (comparison) = " + res);
        if (res)
            Console.WriteLine("val1 = val2");
        else
            Console.WriteLine("val1 != val2");
    }
}

Đầu ra

Chương trình sẽ cho ra kết quả sau:

Value1 (String representation) = 0
Value2 (String representation) = 18446744073709551615
Return value (comparison) = False
val1 != val2