Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0037 Determine the order

To find out order between two string values we can use the CompareTo method from string type. public int CompareTo(string strB) Return Value Condition < 0 This instance precedes strB. 0 This instance has the same position in the sort order as strB. >0 This instance follows strB. -or- strB is null. using System; class Program { static void Main(string[] args) { string s1 = "abc"; string s2 = "def"; int result = s1.CompareTo(s2); Console.WriteLine(result); } } The output: -1