国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

基本概念----Beginning Visual C#

2019-11-17 02:48:46
字體:
來源:轉載
供稿:網友

基本概念----Beginning Visual C#

更多相關文章,見本人的個人主頁:zhongxiewei.com

變量

注釋方式:// 注釋在這里/* 注釋在這里 */

整形變量的類型:

TypeAlias forAllowed Values
sbyteSystem.SByteInteger between -2^7 and 2^7-1
byteSystem.ByteInteger between 0 and 2^8-1
shortSystem.Int16Integer between -2^15 and 2^15-1
ushortSystem.UInt16Integer between 0 and 2^16-1
intSystem.Int32Integer between -2^31 and 2^31-1
uintSystem.UInt32Integer between 0 and 2^32-1
longSystem.Int64Integer between -2^63 and 2^63-1
ulongSystem.UInt64Integer between 0 and 2^64-1

浮點型:

TypeAlias forApPRox Min ValueApprox Max Value
floatSystem.Single 1.5x10-45 3.4x1038
doubleSystem.Double 5.0x10-324 1.7x10308
decimalSystem.Decimal 1.0x10-28 7.9x1028

其他簡單類型:

TypeAlias forAllowed Values
charSystem.CharSingle Unicode char, between 0 and 65535
boolSystem.Booleantrue or false
stringSystem.Stringa 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語言類似

操作符的順序:

PrecedenceOperators
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++類似

更多變量相關

類型轉換

TypeCan safely be converted to
byteshort,ushort,int,uint,long,ulong,float,double,decimal
sbyteshort,int,long,float,double,decimal
shortint,long,float,double,decimal
ushortint,uint,long,ulong,float,double,decimal
intlong,float,double,decimal
uintlong,ulong,float,double,decimal
longfloat,double,decimal
ulongfloat,double,decimal
floatdouble
charushort,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);    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特前旗| 长沙市| 崇文区| 通榆县| 洱源县| 阳信县| 沧源| 巨鹿县| 竹北市| 泰州市| 白河县| 梅州市| 酒泉市| 土默特右旗| 蓝田县| 竹北市| 浦城县| 商丘市| 黄冈市| 苏尼特右旗| 阳谷县| 华池县| 瓦房店市| 台江县| 永和县| 大余县| 汤阴县| 沭阳县| 焉耆| 安图县| 英山县| 金湖县| 永兴县| 四平市| 青浦区| 清新县| 卢氏县| 安多县| 兴化市| 宿松县| 璧山县|