標籤:des style blog http io ar 使用 for strong
更多相關文章,見本人的個人首頁:zhongxiewei.com變數
注釋方式:// 注釋在這裡和/* 注釋在這裡 */
整形變數的類型:
| 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);
或是在項目的選項中,直接開啟預設轉換檢測機制。如所示:
一些複雜的變數類型Enumeration
定義一個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
struct類型和C++不同的是,變數的類型預設不是public的。而是private的。
Arrays
數組的聲明方式如下:<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); }}
對String的操作
string str=" hello world ";常見的有: str.Trim();,str.TrimStart(),str.TrimEnd(),str.ToLower(),str.PadLeft(10, ‘-‘),str.Split({‘ ‘})
練習
- 逆序輸出字串,遞迴的方式完成
public static void printReverse(string str, int i){ if (i < str.Length) { printReverse(str, i + 1); Console.Write(str.Substring(i, 1)); } return;}
基本概念----Beginning Visual C#