注釋方式:// 注釋在這里和/* 注釋在這里 */
整形變量的類型:
| Type | Alias for | Allowed Values |
|---|---|---|
| sbyte | System.SByte | Integer between -2^7 and 2^7-1 |
| byte | System.Byte | Integer between 0 and 2^8-1 |
| short | System.Int16 | Integer between -2^15 and 2^15-1 |
| ushort | System.UInt16 | Integer between 0 and 2^16-1 |
| int | System.Int32 | Integer between -2^31 and 2^31-1 |
| uint | System.UInt32 | Integer between 0 and 2^32-1 |
| long | System.Int64 | Integer between -2^63 and 2^63-1 |
| ulong | System.UInt64 | Integer between 0 and 2^64-1 |
浮點型:
| Type | Alias for | ApPRox Min Value | Approx Max Value |
|---|---|---|---|
| float | System.Single | 1.5x10-45 | 3.4x1038 |
| double | System.Double | 5.0x10-324 | 1.7x10308 |
| decimal | System.Decimal | 1.0x10-28 | 7.9x1028 |
其他簡單類型:
| Type | Alias for | Allowed Values |
|---|---|---|
| char | System.Char | Single Unicode char, between 0 and 65535 |
| bool | System.Boolean | true or false |
| string | System.String | a sequence of characters |
關于變量命名:
對于簡單的變量可以采用camelCase格式,如:firstName,對于一些高級的變量可以采用PascalCase格式,如LastName,這是微軟建議的。
字面常量:
true, false, 100, 100U, 100L, 100UL, 1.5F, 1.5, 1.5M, 'a', "hello"
verbatim, 逐字的常量:
"C://Temp//mydir//myfile.doc"等同于@"C:/Temp/mydir/myfile.doc",另外可以跨行輸入字符串,如:
@"first linesecond linethird line"關于變量的使用,在很多變成語言中都有一個要求,就是在使用前必須進行初始化。
操作符與C語言類似
操作符的順序:
| Precedence | Operators |
|---|---|
| Highest | ++, --(used as prefixes); (), +, -(unary), !, ~ |
| *,/,% | |
| +,- | |
| <<, >> | |
| <,>,<=,>= | |
| ==,!= | |
| & | |
| ^ | |
| | | |
| && | |
| || | |
| =,*=,/=,%=,+=,-=,<<=,>>=,&=,^=,|= | |
| Lowest | ++,--(used as suffixes) |
允許使用goto語句。條件表達式返回的類型必須是bool。如: if (10) return false; // 這句話是不能通過編譯的
在使用switch-case的時候,有一點和c++的用法是不同的,如:
switch(testVar){ case var1: // execute code ... // 如果這里沒有break語句的話,編譯器是不能通過的,而在c++中可以, // 如果想要讓它繼續執行下面的case,必須加上“goto case var2;”語句 // 當然如果case var1下面沒有執行語句的話,也是合理的 case var2: // execute code ... break; default: break;}循環語句和C++類似
| Type | Can safely be converted to |
|---|---|
| byte | short,ushort,int,uint,long,ulong,float,double,decimal |
| sbyte | short,int,long,float,double,decimal |
| short | int,long,float,double,decimal |
| ushort | int,uint,long,ulong,float,double,decimal |
| int | long,float,double,decimal |
| uint | long,ulong,float,double,decimal |
| long | float,double,decimal |
| ulong | float,double,decimal |
| float | double |
| char | ushort,int,uint,long,ulong,float,double,decimal |
除了以上的隱式轉換之外,還存在顯示轉換。為了防止溢出發生,可以用checked(expression)表達式進行處理,如:
byte destVar;short srcVar = 281;destVar = checked((byte)srcVar);
或是在項目的選項中,直接開啟默認轉換檢測機制。如下圖所示:

定義一個enum,如下:
enum orientation : byte // byte能夠被其他的整型類型,如int,long等替換{ north, south, east, west}那么聲明一個枚舉類型采用的方法為:orientation myDirect = orientation.north;;直接輸出myDirect的結果為:north。想要輸出它所表示的byte類型的具體數值,就必須采用顯示的類型轉換:(byte)myDirect。
也可以將“north”字符串轉換成枚舉類型,采用的方式稍微復雜,具體如下:
string myStr = "north";orientation myDirect = (orientation)Enum.Parse(typeof(orientation), myStr);struct類型和C++不同的是,變量的類型默認不是public的。而是private的。
數組的聲明方式如下:<baseType>[] <name>;,如:int[] myIntArray = {1,2,3};,int[] myIntArray = new int[5];。不能夠采用如下的方式進行聲明:<baseType> <name>[];
多維數組的語法結構也有其特殊性。聲明方式如下:<baseType>[,] <name>;,如:double[,] hillHeight = new double[3,4];。在多維數組中各個數據的排序順序為行優先排序,如:
double[,] hillHeight = {{1,2,3,4}, {2,3,4,5}, {3,4,5,6}};foreach (double height in hillHeight){ Console.WriteLine("{0}", height);}// 輸出結果依次為:// [0,0]// [0,1]// ...在當每一行的數據量不相等的時候,可以使用Arrays of Arrays。在使用數組的數組的時候,不能像多維數組一樣進行使用,如:
int[][] jagged;jagged = new int[3][4]; // 在編譯的過程中會出現’cannot implicitly convert type 'int' to 'int[][]'的錯誤
有兩種方式可以用來實現聲明。如:
jagged = new int[2][];jagged[0] = new int[3];jagged[1] = new int[4];// or like belowjagged = {new int[] {1,2,3}, new int[] {1}, new int[] {4,5,6,7}};在對其進行遍歷的時候也需要注意,不能采用如下的方式:
foreach (int val in jagged) // 出現編譯錯誤,不能將int[]轉換成int{ Console.WriteLine(val);}// 于是應該更改為如下方式:foreach (int[] valArray in jagged){ foreach (int val in valArray) { Console.WriteLine(val); }}新聞熱點
疑難解答