WEB开发网
开发学院软件开发C语言 C# 语法练习(5): 语句 阅读

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();
  }
}

Tags:语法 练习 语句

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接