程序执行的操作采用语句表达。 常见操作包括声明变量、赋值、调用方法、循环访问集合,以及根据给定条件分支到一个或另一个代码块。 语句在程序中的执行顺序称为“控制流”或“执行流”。 根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同。
语句可以是以分号结尾的单行代码,也可以是语句块中的一系列单行语句。 语句块括在括号 {} 中,并且可以包含嵌套块。 以下代码演示了两个单行语句示例和一个多行语句块:
public static void Main()
{
// Declaration statement.
int counter;
// Assignment statement.
counter = 1;
// Error! This is an expression, not an expression statement.
// counter + 1;
// Declaration statements with initializers are functionally
// equivalent to declaration statement followed by assignment statement:
int[] radii = [15, 32, 108, 74, 9]; // Declare and initialize an array.
const double pi = 3.14159; // Declare and initialize constant.
// foreach statement block that contains multiple statements.
foreach (int radius in radii)
{
// Declaration statement with initializer.
double circumference = pi * (2 * radius);
// Expression statement (method invocation). A single-line
// statement can span multiple text lines because line breaks
// are treated as white space, which is ignored by the compiler.
System.Console.WriteLine("Radius of circle #{0} is {1}. Circumference = {2:N2}",
counter, radius, circumference);
// Expression statement (postfix increment).
counter++;
} // End of foreach statement block
} // End of Main method body.
} // End of SimpleStatements class.
/*
Output:
Radius of circle #1 = 15. Circumference = 94.25
Radius of circle #2 = 32. Circumference = 201.06
Radius of circle #3 = 108. Circumference = 678.58
Radius of circle #4 = 74. Circumference = 464.96
Radius of circle #5 = 9. Circumference = 56.55
*/
声明语句
以下代码显示了具有和不具有初始赋值的变量声明的示例,以及具有必要初始化的常量声明。
// Variable declaration statements.
double area;
double radius = 2;
// Constant declaration statement.
const double pi = 3.14159;
表达式语句
以下代码显示了表达式语句的示例,包括赋值、使用赋值创建对象和方法调用。
// Expression statement (assignment).
area = 3.14 * (radius * radius);
// Error. Not statement because no assignment:
//circ * 2;
// Expression statement (method invocation).
System.Console.WriteLine();
// Expression statement (new object creation).
System.Collections.Generic.List<string> strings =
new System.Collections.Generic.List<string>();
空语句
以下示例演示了空语句的两种用法:
void ProcessMessages()
{
while (ProcessMessage())
; // Statement needed here.
}
void F()
{
//...
if (done) goto exit;
//...
exit:
; // Statement needed here.
}
嵌入式语句
某些语句如迭代语句的后面始终跟有一条嵌入式语句。 此嵌入式语句可以是单个语句,也可以是语句块中括在括号 {} 内的多个语句。 甚至可以在括号 {} 内包含单行嵌入式语句,如以下示例所示:
// Recommended style. Embedded statement in block.
foreach (string s in System.IO.Directory.GetDirectories(
System.Environment.CurrentDirectory))
{
System.Console.WriteLine(s);
}
// Not recommended.
foreach (string s in System.IO.Directory.GetDirectories(
System.Environment.CurrentDirectory))
System.Console.WriteLine(s);
未括在括号 {} 内的嵌入式语句不能作为声明语句或带标签的语句。 下面的示例对此进行了演示:
if(pointB == true)
//Error CS1023:
int radius = 5;
将该嵌入式语句放在语句块中以修复错误:
if (b == true)
{
// OK:
System.DateTime d = System.DateTime.Now;
System.Console.WriteLine(d.ToLongDateString());
}
嵌套语句块
语句块可以嵌套,如以下代码所示:
foreach (string s in System.IO.Directory.GetDirectories(
System.Environment.CurrentDirectory))
{
if (s.StartsWith("CSharp"))
{
if (s.EndsWith("TempFolder"))
{
return s;
}
}
}
return "Not found.";
无法访问的语句
如果编译器认为在任何情况下控制流都无法到达特定语句,将生成警告 CS0162,如下例所示:
// An over-simplified example of unreachable code.
const int val = 5;
if (val < 4)
{
System.Console.WriteLine("I'll never write anything."); //CS0162
}