Welcome

首页 / 软件开发 / C# / C#语法练习(5): 语句

C#语法练习(5): 语句2011-09-22 博客园 万一if (bool) { } else { }

switch (v) { case v1: ... break; case v2: ... break; ... default: ... break; }

do { } while (bool);

while (bool) { }

for (int i; i < 10; i++) { }

foreach (Type item in items) { }

break;

continue;

goto;

C# 的 switch 语句支持字符串, 但好像只能用 const string 类型的变量.

using System;

class MyClass
{
static void Main()
{
string[] str = { "a", "bb", "ccc" };

const string s1 = "bb";
const string s2 = "ccc";

foreach (string s in str)
{
switch(s)
{
case s1: Console.WriteLine(s.Length); break;
case s2: Console.WriteLine(s.Length); break;
default: Console.WriteLine(s); break;
}
}

Console.ReadKey();
}
}