C# 语法练习(5): 语句
2009-02-23 08:17:02 来源:WEB开发网核心提示:if (bool) { } else { }switch (v) { case v1: ... break; case v2: ... break; ... default: ... break; }do { } while (bool);while (bool) { }for (int i; i < 10; i
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();
}
}
[]
更多精彩
赞助商链接