看板 C_Sharp 關於我們 聯絡資訊
我看到一書「Windows Forms Programming using C#」812頁中寫道 struct與class在記憶體中的不同: struct vInt { pubilc int data; } vInt v1 = new vInt(); vInt v2 = new vInt(); v1.data = 5; v2 = v1; v1.data = 7 // v2.data = 5 (不變) 但class卻不同: class rInt { pubilc int data; } rInt r1 = new rInt(); rInt r2 = new rInt(); r1.data = 5; r2 = r1; r1.data = 7 // r2.data = 7 (跟著改變) 以上是因為struct屬於數值型別,其l-value是"值"而非"址"! 這種特性適合value-type的int. 因為若用class代替struct,很容易讓值被更換掉哩! -- 貫徹分享精神 我為人人,人人為我 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.248.89.46
liunate:恩...教科書寫說 struct是輕量級的class 203.73.234.149 07/11