第二堂課:Visual C# 2010變數宣告方式與程式語法、了解基本型別並於使用時挑選適當的型別來儲存資料
===第三章 變數與資料型別 ====================================================
重點整理:
.Value Type / Reference Type .無論Value Type 或是 Referenct Type。
所有的資料型別皆是繼承System.Object,所以可以使用System.Object的方式來操作它
=>ex: 10.ToString() , 10.GetType()
.value type => ex: int , float , double,char,bool … =>長度(size)是固定的
.value type => 會自動轉為System.____ 是 common type system(CTS),用於跨語言溝通(VB <=> C#)
.struct:為輕量的object, 不可以繼承。
.struct 中的 constructor ,不可以宣告”預設”的建構函式。
因為它不是reference,所以不需要new。 所以宣告預設的建構函式並沒有功用。
在struct中用建構函式,是為了要快速宣告初始值而已。
並不會建立Object.而是會跳入執行該method而已。
.int i = new int(); =>是合法的宣告初始值。i在此會初始為 0。
DateTime d = new DateTime(); //default constructor
.enum 列舉型別資料=>繼承ValueType
a.使用.GetHashCode() 可取得整數值。
b.使用explicit , (int) 也可取得整數值。
.anonymous type 匿名型別
=> var
=> .net 3.0 以上
=> 一定要有初始值。編譯器才能推測資料型態。
=> 只能用在區域變數
Post your comments