UInt64.ToString() Method in C# with Examples

The UInt64.ToString() method in C# is used to convert the numeric value of the current UInt64 instance to its equivalent string representation. This method is particularly useful when you need to display UInt64 values as text or perform string operations on numeric data.

Syntax

Following is the syntax for the parameterless overload −

public override string ToString();

The method also has additional overloads for custom formatting −

public string ToString(string format);
public string ToString(IFormatProvider provider);
public string ToString(string format, IFormatProvider provider);

Return Value

Returns a string that represents the value of the current UInt64 instance.

Using Basic ToString() Method

The simplest form of ToString() converts a UInt64 value to its string representation without any special formatting −

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");
    }
}

The output of the above code is −

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

Using ToString() with Extreme Values

This example demonstrates ToString() with the minimum and maximum values of UInt64 −

using System;

public class Demo {
    public static void Main() {
        ulong val1 = UInt64.MinValue;
        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");
    }
}

The output of the above code is −

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

Using ToString() with Format Specifiers

You can use format specifiers to control how the UInt64 value is converted to string −

using System;

public class Demo {
    public static void Main() {
        ulong value = 123456789;
        
        Console.WriteLine("Default: " + value.ToString());
        Console.WriteLine("Currency: " + value.ToString("C"));
        Console.WriteLine("Decimal: " + value.ToString("D"));
        Console.WriteLine("Exponential: " + value.ToString("E"));
        Console.WriteLine("Fixed-point: " + value.ToString("F2"));
        Console.WriteLine("Hexadecimal: " + value.ToString("X"));
    }
}

The output of the above code is −

Default: 123456789
Currency: $123,456,789.00
Decimal: 123456789
Exponential: 1.234568E+008
Fixed-point: 123456789.00
Hexadecimal: 75BCD15

Conclusion

The UInt64.ToString() method provides a simple way to convert 64-bit unsigned integer values to their string representations. It supports various formatting options and is essential for displaying numeric data as text in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

532 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements