本文主要介紹C#最基本要件和運算式,以供有興趣的朋友參考。
Statements
Statement為C#最基本的組成,所有程式都是由Statement建構的,可以宣告區域變數、常數、呼叫方法、建立物件,或指派值給變數、屬性或欄位。
Statement通常以分號結尾。
常見的Statements
分類
|
C# 關鍵字
|
Declaration statements
|
xx=2
|
Expression statements
|
area = 3.14 * (radius * radius);
|
Selection statements
|
if、else、switch、case
|
Iteration statements
|
do、for、foreach、in、while
|
Jump statements
|
break、continue、default、goto、return、yield
|
Exception handling statements
|
throw、try-catch、try-finally、try-catch-finally
|
Msdn Example:
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 pointA 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 pointA 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.
Expression
運算式是程式碼片段,可判定為單一的值、物件、方法或命名空間。
運算式可包含常值、方法引動過程、運算子和運算元,或「簡單名稱」(Simple Name)。簡單名稱可以是變數的名稱、型別成員、方法參數、命名空間或型別。
Msdn example:
((x < 10) && ( x > 5)) || ((x > 20) && (x < 25))
System.Convert.ToInt32("35")
文章標籤
全站熱搜
留言列表