The CompareTo method in C# is employed to compare a String instance with a specific String object. It determines if this String instance comes before, after, or is at the same position in the sorting order as the specified string.
Signature
Example
public int CompareTo(String str)
public int CompareTo(Object)
Parameters
The "str" parameter is a string argument utilized for comparison purposes.
Return
It returns an integer value.
C# String CompareTo Method Example
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "hello";
string s2 = "hello";
string s3 = "csharp";
Console.WriteLine(s1.CompareTo(s2));
Console.WriteLine(s2.CompareTo(s3));
}
}
Output: