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
Int32.CompareTo Method in C# with Examples
The Int32.CompareTo method in C# is used to compare the current integer instance with another integer or object and returns an indication of their relative values. This method is particularly useful for sorting operations and conditional comparisons.
Syntax
The Int32.CompareTo method has two overloads −
public int CompareTo(int value); public int CompareTo(object value);
Parameters
-
value − An integer or object to compare with the current instance.
Return Value
| Return Value | Condition | Meaning |
|---|---|---|
| Less than zero (< 0) | Current instance < value | Current instance is smaller |
| Zero (0) | Current instance = value | Both values are equal |
| Greater than zero (> 0) | Current instance > value | Current instance is larger |
Using CompareTo with Integers
Example 1 - Comparing Different Values
using System;
public class Demo {
public static void Main() {
int val1 = 20;
int 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 2 - Comparing Equal Values
using System;
public class Demo {
public static void Main() {
int val1 = 50;
int val2 = 50;
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 = 50 Value 2 = 50 Return value (comparison) = 0
Using CompareTo with Object Parameter
Example 3 - Comparing with Object
using System;
public class Demo {
public static void Main() {
int val1 = 15;
object val2 = 25;
Console.WriteLine("Value 1 = " + val1);
Console.WriteLine("Value 2 = " + val2);
Console.WriteLine("Return value (comparison) = " + val1.CompareTo(val2));
// Demonstrating all three cases
int a = 10, b = 20, c = 10;
Console.WriteLine("\nComparison Results:");
Console.WriteLine("10.CompareTo(20) = " + a.CompareTo(b)); // Returns -1
Console.WriteLine("20.CompareTo(10) = " + b.CompareTo(a)); // Returns 1
Console.WriteLine("10.CompareTo(10) = " + a.CompareTo(c)); // Returns 0
}
}
The output of the above code is −
Value 1 = 15 Value 2 = 25 Return value (comparison) = -1 Comparison Results: 10.CompareTo(20) = -1 20.CompareTo(10) = 1 10.CompareTo(10) = 0
Common Use Cases
The CompareTo method is commonly used in sorting algorithms, conditional statements, and implementing custom comparison logic. It provides a standardized way to determine the relative ordering of integer values, making it essential for data structure operations and algorithmic implementations.
Conclusion
The Int32.CompareTo method provides a reliable way to compare integer values, returning negative, zero, or positive values based on the comparison result. It supports both integer and object parameters, making it versatile for various programming scenarios involving numeric comparisons.
