Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Int16.CompareTo() Method in C#
The Int16.CompareTo() method in C# is used to compare a 16-bit signed integer (short) with another value and returns an integer that indicates their relative order. This method is essential for sorting operations and determining the relationship between two Int16 values.
Syntax
The Int16.CompareTo() method has two overloads −
public int CompareTo(short value); public int CompareTo(object value);
Parameters
-
value − A 16-bit signed integer or an object to compare with the current instance.
Return Value
The method returns an integer with the following meaning −
| Return Value | Condition | Meaning |
|---|---|---|
| Less than zero | Current instance < value | This instance is smaller |
| Zero | Current instance = value | Both values are equal |
| Greater than zero | Current instance > value | This instance is larger |
Using CompareTo with Int16 Values
Example - Different Values
using System;
public class Demo {
public static void Main() {
short val1 = 20;
short val2 = 18;
Console.WriteLine("Value 1 = " + val1);
Console.WriteLine("Value 2 = " + val2);
Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
}
}
The output of the above code is −
Value 1 = 20 Value 2 = 18 Return value (comparison) = 1
Example - Equal Values
using System;
public class Demo {
public static void Main() {
short val1 = 10;
short val2 = 10;
Console.WriteLine("Value 1 = " + val1);
Console.WriteLine("Value 2 = " + val2);
Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
}
}
The output of the above code is −
Value 1 = 10 Value 2 = 10 Return value (comparison) = 0
Using CompareTo with Object Parameter
Example
using System;
public class Demo {
public static void Main() {
short val1 = 15;
object val2 = (short)25;
Console.WriteLine("Value 1 = " + val1);
Console.WriteLine("Value 2 = " + val2);
Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
// Comparing with null
Console.WriteLine("Comparing with null = " + val1.CompareTo(null));
}
}
The output of the above code is −
Value 1 = 15 Value 2 = 25 Return value (comparison) = -1 Comparing with null = 1
Common Use Cases
The CompareTo() method is commonly used in sorting algorithms, finding minimum or maximum values, and implementing custom comparison logic in data structures.
Example - Sorting Array
using System;
public class Demo {
public static void Main() {
short[] numbers = {30, 10, 25, 5, 15};
Console.WriteLine("Original array:");
foreach(short num in numbers) {
Console.Write(num + " ");
}
Array.Sort(numbers);
Console.WriteLine("\nSorted array:");
foreach(short num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original array: 30 10 25 5 15 Sorted array: 5 10 15 25 30
Conclusion
The Int16.CompareTo() method provides a standardized way to compare 16-bit signed integers, returning negative, zero, or positive values to indicate their relative order. This method is fundamental for sorting operations and implementing comparison-based algorithms with Int16 values.
