UInt64 Struct in C#

The UInt64 struct in C# represents a 64-bit unsigned integer with values ranging from 0 to 18,446,744,073,709,551,615. This struct provides various methods for comparison, equality checks, and other operations on unsigned 64-bit integers.

UInt64 Value Range 0 18,446,744,073,709,551,615 64-bit Unsigned Integer 2^64 - 1 possible values

UInt64.CompareTo() Method

The UInt64.CompareTo() method compares the current instance to a specified object or UInt64 value and returns an indication of their relative values.

Syntax

public int CompareTo(object val);
public int CompareTo(ulong val);

Parameters

  • val − An object or unsigned 64-bit integer to compare with the current instance.

Return Value

  • 0 − Current instance equals the specified value

  • Less than 0 − Current instance is less than the specified value

  • Greater than 0 − Current instance is greater than the specified value

Example

using System;

public class Demo {
    public static void Main() {
        ulong val1 = 257876;
        ulong val2 = 5657655;
        int res = val1.CompareTo(val2);
        Console.WriteLine("Return value (comparison) = " + res);
        
        if (res > 0)
            Console.WriteLine("val1 > val2");
        else if (res < 0)
            Console.WriteLine("val1 < val2");
        else
            Console.WriteLine("val1 = val2");
    }
}

The output of the above code is −

Return value (comparison) = -1
val1 < val2

Using CompareTo() with Object Parameter

using System;

public class Demo {
    public static void Main() {
        ulong val1 = 258768768;
        object val2 = (ulong)1765765;
        int res = val1.CompareTo(val2);
        Console.WriteLine("Return value (comparison) = " + res);
        
        if (res > 0)
            Console.WriteLine("val1 > val2");
        else if (res < 0)
            Console.WriteLine("val1 < val2");
        else
            Console.WriteLine("val1 = val2");
    }
}

The output of the above code is −

Return value (comparison) = 1
val1 > val2

UInt64.Equals() Method

The UInt64.Equals() method returns a boolean value indicating whether the current instance is equal to a specified object or UInt64 value.

Syntax

public override bool Equals(object ob);
public bool Equals(ulong ob);

Parameters

  • ob − An object or 64-bit unsigned integer to compare with the current instance.

Return Value

  • true − If the current instance equals the specified value

  • false − If the current instance does not equal the specified value

Example

using System;

public class Demo {
    public static void Main() {
        ulong val1 = 44565777;
        ulong val2 = 77878787;
        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 −

Return value (comparison) = False
val1 != val2

Using Equals() with Equal Values

using System;

public class Demo {
    public static void Main() {
        ulong val1 = 78796878;
        ulong val2 = 78796878;
        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 −

Return value (comparison) = True
val1 = val2

Comparison of Methods

Method Return Type Purpose
CompareTo() int Compares values and returns relative ordering (-1, 0, 1)
Equals() bool Checks for equality and returns true or false

Conclusion

The UInt64 struct provides essential comparison methods like CompareTo() for ordering and Equals() for equality checks. These methods are fundamental for sorting, searching, and conditional operations with 64-bit unsigned integers in C# applications.

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

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements