C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
CompareTo results
5.CompareTo(6) = -1 First int is smaller.
6.CompareTo(5) = 1 First int is larger.
5.CompareTo(5) = 0 Ints are equal.
Here: We see that when the first number is larger, the result is 1. When the first number is smaller, the result is -1.
And: When the numbers are equal, the result is 0. These values are written to the console.
C# program that uses CompareTo method on int
using System;
class Program
{
static void Main()
{
const int a = 5;
const int b = 6;
const int c = 5;
int ab = a.CompareTo(b);
int ba = b.CompareTo(a);
int ca = c.CompareTo(a);
Console.WriteLine(ab);
Console.WriteLine(ba);
Console.WriteLine(ca);
}
}
Output
-1
1
0