语句Statements

    使用代码块,可以在允许编写一个语句的上下文中编写多个语句。代码块是由一系列在分隔符 和 } 内编写的语句组成。

    声明语句用于声明局部变量和常量。

    表达式语句用于计算表达式。可用作语句的表达式包括方法调用、使用 new 运算符的对象分配、使用 = 和复合赋值运算符的赋值、使用 ++ 运算符和 await 表达式的递增和递减运算。

    迭代语句用于重复执行嵌入语句。这一类语句包括 whiledoforforeach 语句。

    跳转语句用于转移控制权。这一类语句包括 breakcontinuegotothrowreturnyield 语句。

    trycatch 语句用于捕获在代码块执行期间发生的异常,tryfinally 语句用于指定始终执行的最终代码,无论异常发生与否。

    lock 语句用于获取给定对象的相互排斥锁定,执行语句,然后解除锁定。

    using 语句用于获取资源,执行语句,然后释放资源。

    下面列出了可以使用的各种语句,以及每种语句的示例。

    • 局部变量声明:
    • 局部常量声明:
    1. static void ConstantDeclarations(string[] args)
    2. {
    3. const float pi = 3.1415927f;
    4. const int r = 25;
    5. Console.WriteLine(pi * r * r);
    6. }
    • 表达式语句:
    1. static void Expressions(string[] args)
    2. {
    3. int i;
    4. i = 123; // Expression statement
    5. Console.WriteLine(i); // Expression statement
    6. i++; // Expression statement
    7. Console.WriteLine(i); // Expression statement
    8. }
    • if 语句:
    1. static void IfStatement(string[] args)
    2. {
    3. if (args.Length == 0)
    4. {
    5. Console.WriteLine("No arguments");
    6. }
    7. else
    8. {
    9. Console.WriteLine("One or more arguments");
    10. }
    11. }
    • switch 语句:
    1. static void SwitchStatement(string[] args)
    2. {
    3. int n = args.Length;
    4. switch (n)
    5. {
    6. case 0:
    7. Console.WriteLine("No arguments");
    8. break;
    9. case 1:
    10. Console.WriteLine("One argument");
    11. break;
    12. default:
    13. Console.WriteLine($"{n} arguments");
    14. break;
    15. }
    16. }
    • while 语句:
    1. static void WhileStatement(string[] args)
    2. int i = 0;
    3. while (i < args.Length)
    4. {
    5. Console.WriteLine(args[i]);
    6. i++;
    7. }
    8. }
    • do 语句:
    • for 语句:
    1. static void ForStatement(string[] args)
    2. {
    3. for (int i = 0; i < args.Length; i++)
    4. {
    5. }
    6. }
    • foreach 语句:
    1. static void ForEachStatement(string[] args)
    2. {
    3. foreach (string s in args)
    4. {
    5. Console.WriteLine(s);
    6. }
    7. }
    • break 语句:
    1. static void BreakStatement(string[] args)
    2. {
    3. while (true)
    4. {
    5. string s = Console.ReadLine();
    6. if (string.IsNullOrEmpty(s))
    7. break;
    8. Console.WriteLine(s);
    9. }
    10. }
    • continue 语句:
    1. static void ContinueStatement(string[] args)
    2. {
    3. for (int i = 0; i < args.Length; i++)
    4. {
    5. if (args[i].StartsWith("/"))
    6. continue;
    7. Console.WriteLine(args[i]);
    8. }
    9. }
    • goto 语句:
    1. static void GoToStatement(string[] args)
    2. {
    3. int i = 0;
    4. goto check;
    5. loop:
    6. Console.WriteLine(args[i++]);
    7. check:
    8. if (i < args.Length)
    9. goto loop;
    10. }
    • return 语句:
    • yield 语句:
    1. static System.Collections.Generic.IEnumerable<int> Range(int from, int to)
    2. {
    3. for (int i = from; i < to; i++)
    4. {
    5. yield return i;
    6. }
    7. yield break;
    8. }
    9. static void YieldStatement(string[] args)
    10. {
    11. foreach (int i in Range(-10,10))
    12. {
    13. Console.WriteLine(i);
    14. }
    • throwtry 语句:
    1. static double Divide(double x, double y)
    2. {
    3. if (y == 0)
    4. return x / y;
    5. }
    6. static void TryCatch(string[] args)
    7. {
    8. try
    9. {
    10. if (args.Length != 2)
    11. {
    12. throw new InvalidOperationException("Two numbers required");
    13. }
    14. double x = double.Parse(args[0]);
    15. double y = double.Parse(args[1]);
    16. Console.WriteLine(Divide(x, y));
    17. }
    18. catch (InvalidOperationException e)
    19. {
    20. Console.WriteLine(e.Message);
    21. }
    22. finally
    23. {
    24. Console.WriteLine("Good bye!");
    25. }
    26. }
    • checkedunchecked 语句:
    1. static void CheckedUnchecked(string[] args)
    2. {
    3. int x = int.MaxValue;
    4. unchecked
    5. {
    6. Console.WriteLine(x + 1); // Overflow
    7. }
    8. checked
    9. {
    10. Console.WriteLine(x + 1); // Exception
    11. }
    12. }
    • lock 语句:
    1. class Account
    2. {
    3. decimal balance;
    4. private readonly object sync = new object();
    5. public void Withdraw(decimal amount)
    6. {
    7. lock (sync)
    8. {
    9. if (amount > balance)
    10. {
    11. throw new Exception(
    12. "Insufficient funds");
    13. }
    14. balance -= amount;
    15. }
    16. }
    17. }
    • using 语句:
    1. static void UsingStatement(string[] args)
    2. {
    3. using (TextWriter w = File.CreateText("test.txt"))
    4. {
    5. w.WriteLine("Line one");
    6. w.WriteLine("Line two");
    7. w.WriteLine("Line three");
    8. }