![]() |
VOOZH | about |
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.
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.
There are 2 methods in the overload list of this method:
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.
Geek is less than Geeks
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.
32