Last Updated : 19 May, 2025
In C#, the Compare method is used to compare two objects or strings and returns an indication of their relative sort order.
Example: C# program to illustrate how to use StringComparer() to perform ordinal comparison.
C#
// C# program to illustrate the
// CompareOrdinal(String, Int32,
// String, Int32, Int32) method
using System;
class Geeks
{
static public void Main()
{
string s1 = "GFG";
string s2 = "GFG";
int res = StringComparer.Ordinal.Compare(s1, s2);
if (res < 0)
Console.WriteLine($"{s1} is less than {s2}");
else if (res > 0)
Console.WriteLine($"{s1} is greater than {s2}");
else
Console.WriteLine($"{s1} is equal to {s2}");
}
}
GFG is equal to GFG
Explanation: In this example, the StringComparer.Ordinal.Compare() method checks if two strings are equal. It returns 0 when both strings are identical, a negative value if the first string is less, and a positive value if it is greater.
Overloads of StringComparer.Compare() MethodThere are 2 methods in the overload list of this method:
1. Compare(Object a, Object b)This method compares two objects and returns an indication of their relative sort order when overridden in a derived class.
Syntax:
public int Compare (object a, object b);
Note: This method throws an ArgumentException if the objects being compared are not of the same type.
Example: In this example, we are comparing two objects using the StringComparer.Compare(Object, Object) method.
C#
// Demonstrating the working of
// StringComparer.Compare() method
using System;
class Geeks
{
static void Main()
{
// Creating two objects holding string
// values "Geek" and "Geeks"
object obj1 = "Geek";
object obj2 = "Geeks";
// Using StringComparer.Ordinal
// to compare the two string objects
int result = StringComparer.Ordinal.Compare(obj1, obj2);
// Checking the result of comparison and
// printing appropriate message
if (result < 0)
Console.WriteLine($"{obj1} is less than {obj2}");
else if (result > 0)
Console.WriteLine($"{obj1} is greater than {obj2}");
else
Console.WriteLine($"{obj1} is equal to {obj2}");
}
}
Geek is less than Geeks2. Compare(String a, String b)
This method compares two strings and returns an indication of their relative sort order when overridden in a derived class.
Syntax:
public abstract int Compare (string a, string b);
Note: This method throws an ArgumentException
Example: In this example, we are comparing two string objects using the StringComparer.Compare(String, String) method.
C#
// Demonstrating the working of
// Compare(String a, String b) method
using System;
class Geeks
{
static void Main()
{
// Define two strings to compare
string s1 = "geek";
string s2 = "Geek";
// Compare the two strings using
// ordinal (Unicode) comparison
int result = StringComparer.Ordinal.Compare(s1, s2);
// Print the comparison result
// Positive number means s1 > s2,
// negative means s1 < s2,
// zero means equal
Console.WriteLine(result);
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4