using System; class IfTest2App { const string CPlusPlus = "C++"; const string VisualBasic = "Visual Basic"; const string Java = "Java"; public static void Main() { Console.Write("What is your current language of choice " + "(excluding C#)?"); string inputString = Console.ReadLine(); if (0 == String.Compare(inputString, CPlusPlus, true)) { Console.WriteLine("\nYou'll have no problem picking " + "up C# !"); } else if (0 == String.Compare(inputString, VisualBasic, true)) { Console.WriteLine("\nYou'll find lots of cool VB features " + "in C# !"); } else if (0 == String.Compare(inputString, Java, true)) { Console.WriteLine("\nYou'll have an easier time " + "picking up C# <G> !!"); } else { Console.WriteLine("\nSorry - doesn't compute."); } } }
Notice the use of the == operator to compare 0 to the returned value of String.Compare. This is because String.Compare will return -1 if the first string is less than the second string, 1 if the first string is greater than the second, and 0 if the two are identical. However, this illustrates some interesting details, described in the following section, about how C# enforces use of the if statement.