UInt64.CompareTo() Method in C# with Examples

The UInt64.CompareTo() method in C# is used to compare the current instance to a specified object or UInt64 and returns an indication of their relative values. This method is essential for sorting operations and numerical comparisons with 64-bit unsigned integers.

Syntax

The UInt64.CompareTo() method has two overloads −

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

The method returns an integer that indicates the relative position of the current instance compared to the parameter −

  • 0 − The current instance is equal to the value.

  • Less than zero − The current instance is less than the value.

  • Greater than zero − The current instance is greater than the value.

Using CompareTo() with UInt64 Values

This example demonstrates comparing two UInt64 values directly −

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

This example shows how to use the object overload of the method −

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

Common Use Cases

The CompareTo() method is commonly used in sorting algorithms and when implementing custom comparison logic −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      List<ulong> numbers = new List<ulong> { 9999999999, 1000000000, 5000000000 };
      
      numbers.Sort((x, y) => x.CompareTo(y));
      
      Console.WriteLine("Sorted numbers:");
      foreach (ulong num in numbers) {
         Console.WriteLine(num);
      }
   }
}

The output of the above code is −

Sorted numbers:
1000000000
5000000000
9999999999

Conclusion

The UInt64.CompareTo() method provides a reliable way to compare 64-bit unsigned integers and returns standardized integer values indicating their relative order. This method is particularly useful in sorting operations and custom comparison implementations where you need to determine the relationship between two UInt64 values.

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

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements