SWITCH语句类似于IF语句,根据条件选择执行代码,本示例根据输入的名字,执行相应的问候代码。
using System; namespace a { class Program { public static void Main(string[] args) { const string myName="karli"; const string sexyName="angelina"; const string sillyName="ploppy"; string name; Console.WriteLine("What is your name?"); name=Console.ReadLine(); switch (name.ToLower()) { case myName: Console.WriteLine("You have the same name as me!"); break; case sexyName: Console.WriteLine("My,what a sexy name you have!"); break; case sillyName: Console.WriteLine("The's a very silly name."); break; } Console.WriteLine("Hello {0}!",name); Console.ReadKey(); } } }