UInt16.CompareTo() Method in C# with Examples

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

Syntax

The UInt16.CompareTo() method has two overloads −

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

Parameters

  • val − In the first overload, it is an object to compare. In the second overload, it is an unsigned 16-bit integer to compare.

Return Value

The method returns an integer that indicates the relative position of the current instance and the value parameter −

  • 0 − The current instance is equal to the value

  • Less than 0 − The current instance is less than the value

  • Greater than 0 − The current instance is greater than the value

CompareTo() Return Values < 0 Current value is less than compared value = 0 Current value is equal to compared value > 0 Current value is greater than compared value

Using CompareTo() with UInt16 Values

Example

using System;

public class Demo {
   public static void Main() {
      ushort val1 = 25;
      ushort val2 = 55;
      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

Example

using System;

public class Demo {
   public static void Main() {
      ushort val1 = 25;
      object val2 = (ushort)2;
      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() for Sorting

Example

using System;

public class Demo {
   public static void Main() {
      ushort[] numbers = { 100, 50, 25, 75, 10 };
      
      Console.WriteLine("Original array:");
      foreach (ushort num in numbers) {
         Console.Write(num + " ");
      }
      
      Array.Sort(numbers);
      
      Console.WriteLine("\nSorted array:");
      foreach (ushort num in numbers) {
         Console.Write(num + " ");
      }
   }
}

The output of the above code is −

Original array:
100 50 25 75 10 
Sorted array:
10 25 50 75 100

Conclusion

The UInt16.CompareTo() method provides a standardized way to compare unsigned 16-bit integer values, returning negative, zero, or positive values based on the comparison result. This method is particularly useful for sorting operations and implementing custom comparison logic in applications.

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

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements