我把重點整理一下:
(1)struct是value type, class是reference type。所以一樣的data member struct會比class小一點(少消耗了位址空間)
例如:
Class c1
Private Integer a
Private Integer b
End Class
Struct s1
Private Integer a
Private Integer b
End Struct
Dim x as new c1()
Dim y as s1
x會比y大。因為x會比y多佔一個指向x的data member的位址。
(2)struct在CallByValue傳遞時會複製整個結構成員, class則只會copy位址,但是兩個位址會指到相同記憶體區塊
也就是說即使是ByVal對class的instance來說還是同等ByRef!
(3)class因屬reference type,需額外負擔Garbage collection回收、heap管理機制
這一點struct就把class比下去了
(4)成員函式(method member)不影響評估struct與class
對這兩個來說,成員函式都只會存在一副,不管今天有幾個instance
(5)成員變數少(小)時,用struct會比較有利
這點呼應(3),但是如果很大時可以考慮用class
(6)struct不能繼承
要用到繼承只能靠class
(7)對事件處理,struct比class有更多限制
WithEvents與Handles 這兩個關鍵字不可套在struct中
沒有留言:
張貼留言