1.1 C#程序的基本结构和基本语法要点
Here, you’ll take a closer look at the console application example and break down the structure a bit. Here’s the code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Output text to the screen. Console.WriteLine("The first app in Beginning C# Programming!"); Console.ReadKey(); } } }
所有的C#程序后缀为.cs
编辑时,为使用代码大纲(代码折叠)功能,可如下:
#region Using directives using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; #endregion
以#开头的内容可视为预指令,他不是C#的关键字。编辑时代码可折叠为1行。
区分大小写。
语句中的空格将不予考虑。
分号“;”为一条语句的结尾。一条语句可书写在2行或多行。
声明语句后面不要分号“;”
注释的方式有三种:
(1)/* */
特点:以“/*”开始,可书写于多行,只直到有“*/”结束。
(2)//
特点:以“//”开头,只能书写于一行。可为单独的一行,也可以放在一条语句的分号之后。
(3)///
与//相同。不同的是该方法可由VS提取内容。
占位符标签
程序中的占位符标签类似于汇编语言中的程序指针地址。下图中第2行和第1行为一个标签,因其间无分号相隔。
<code line 1, statement 1>; <code line 2, statement 2> <code line 3, statement 2>;
1.2 语句
1.2.1 跳转语句
?goto 语句
The goto statement is used as follows:
goto <labelName>;
Labels are defi ned as follows:
<labelName>:
For example, consider the following:
int myInteger = 5; goto myLabel; myInteger += 10; myLabel: Console.WriteLine("myInteger = {0}", myInteger);
1.2.2 分支语句
? The ternary operator 三元运算符
? The if statement if语句
? The switch statement switch语句
1.2.2.1 三元运算符
常用于简单赋值,较复杂的代码宜用if语句。
The syntax is asfollows:
<test> ? <resultIfTrue>: <resultIfFalse>
Here,
<test> is evaluated to obtain a Boolean value, and the result of the operator is either <resultIfTrue> or <resultIfFalse> based on this value.
You might use this as follows to test the value of an int variable called myInteger:
string resultString = (myInteger < 10) ? "Less than 10" : "Greater than or equal to 10";
如果myInteger<10,则:resultString = "Less than 10"
如果myInteger≥10,则:resultString = "Greater than or equal to 10"
1.2.2.2 if语句
The syntax is asfollows:
if (<test>) <code executed if <test> is true>;
if (<test>) <code executed if <test> is true>; else <code executed if <test> is false>;
if (<test>) { <code executed if <test> is true>; } else { <code executed if <test> is false>; }
举例:
static void Main(string[] args) { string comparison; Console.WriteLine("Enter a number:"); double var1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter another number:"); double var2 = Convert.ToDouble(Console.ReadLine()); if (var1 < var2) comparison = "less than"; else { if (var1 == var2) comparison = "equal to"; else comparison = "greater than"; } Console.WriteLine("The first number is {0} the second number.",comparison); Console.ReadKey(); }
举例:判断更多的条件:
if (var1 == 1) { // Do something. } else { if (var1 == 2) { // Do something else. } else { if (var1 == 3 || var1 == 4) { // Do something else. } else { // Do something else. } } }
1.2.2.3 switch语句
标准语法:The basic structure of a switch statement is as follows:
switch (<testVar>) { case <comparisonVal1>: <code to execute if <testVar> == <comparisonVal1> > break; case <comparisonVal2>: <code to execute if <testVar> == <comparisonVal2> > break; ... case <comparisonValN>: <code to execute if <testVar> == <comparisonValN> > break; default: <code to execute if <testVar> != comparisonVals> break; }
使用技巧:
{ case <comparisonVal1>: <code to execute if <testVar> == <comparisonVal1> > goto case <comparisonVal2>; case <comparisonVal2>: <code to execute if <testVar> == <comparisonVal2> > break; ...
switch (<testVar>) { case <comparisonVal1>: case <comparisonVal2>: <code to execute if <testVar> == <comparisonVal1> or <testVar> == <comparisonVal2> > break; ...
switch (myInteger) { case 1: <code to execute if myInteger == 1> break; case −1: <code to execute if myInteger == −1> break; default: <code to execute if myInteger != comparisons> break; }
1.2.3 循环语句
? do循环
? while循环
? for循环
循环的中断
无限循环
1.2.3.1 do循环
基本语法:
do { <code to be looped> } while (<Test>);
举例:
int i = 1; do { Console.WriteLine("{0}", i++); } while (i <= 10);
1.2.3.2 while循环
语法:
while (<Test>) { <code to be looped> }
举例
int i = 1; while (i <= 10) { Console.WriteLine("{0}", i++); }
1.2.3.3 for循环
语法:
for (<initialization>; <condition>; <operation>) { <code to loop> }
举例:
int i; for (i = 1; i <= 10; ++i) { Console.WriteLine("{0}", i); }
1.4