[英]How boxing a value type work internally in C#?
我知道什么是裝箱/拆箱,但不太確定它是如何在內部實現的,假設我們有以下代碼:
struct Point {
public Int32 x, y;
}
...
ArrayList a = new ArrayList();
Point p; // Allocate a Point (not in the heap).
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i; // Initialize the members in the value type.
a.Add(p); // Box the value type and add the reference to the Arraylist.
}
以下是對所發生情況的一般描述:
Point值類型必須轉換為真正的堆管理object,並且必須獲得對這個object的引用。在運行時,將當前駐留在Point值類型實例p中的字段復制到新分配的Point ZA8CFDE6331BD49EB2AC96F89111666 裝箱點 object(現在是引用類型)的地址被返回,然后傳遞給 Add 方法。 點 object 將保留在堆中,直到它被垃圾回收。
所以我的假設是,當編譯器編譯代碼並檢測到需要裝箱時,它會生成 IL 代碼(為便於閱讀,采用 C# 格式):
ArrayList a = new ArrayList();
Point p;
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i;
Wrapper w = new Wrapper(p); // Wrapper w is a dynamic generate class instance that takes a Point struct instance to copy its fields internally, I know it is not the exact correct format to express, but you get my idea.
a.Add(w);
}
Q1-我的假設正確嗎?
Q2-如果我的假設是正確的,這意味着p
仍然存在於堆棧中,我們可以將p
作為結構實例重用,這會稍微影響性能,因為p
仍然存在於堆棧中,非常多余,但由於堆棧是一個短壽命數據結構快速展開,所以我們並不真正關心冗余結構實例,因為影響是如此之小以至於不需要考慮它?
我知道什么是裝箱/拆箱,但不太確定它是如何在內部實現的,假設我們有以下代碼:
struct Point {
public Int32 x, y;
}
...
ArrayList a = new ArrayList();
Point p; // Allocate a Point (not in the heap).
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i; // Initialize the members in the value type.
a.Add(p); // Box the value type and add the reference to the Arraylist.
}
以下是對所發生情況的一般描述:
Point值類型必須轉換為真正的堆管理object,並且必須獲得對這個object的引用。在運行時,將當前駐留在Point值類型實例p中的字段復制到新分配的Point ZA8CFDE6331BD49EB2AC96F89111666 裝箱點 object(現在是引用類型)的地址被返回,然后傳遞給 Add 方法。 點 object 將保留在堆中,直到它被垃圾回收。
所以我的假設是,當編譯器編譯代碼並檢測到需要裝箱時,它會生成 IL 代碼(為便於閱讀,采用 C# 格式):
ArrayList a = new ArrayList();
Point p;
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i;
Wrapper w = new Wrapper(p); // Wrapper w is a dynamic generate class instance that takes a Point struct instance to copy its fields internally, I know it is not the exact correct format to express, but you get my idea.
a.Add(w);
}
Q1-我的假設正確嗎?
Q2-如果我的假設是正確的,這意味着p
仍然存在於堆棧中,我們可以將p
作為結構實例重用,這會稍微影響性能,因為p
仍然存在於堆棧中,非常多余,但由於堆棧是一個短壽命數據結構快速展開,所以我們並不真正關心冗余結構實例,因為影響是如此之小以至於不需要考慮它?
我知道什么是裝箱/拆箱,但不太確定它是如何在內部實現的,假設我們有以下代碼:
struct Point {
public Int32 x, y;
}
...
ArrayList a = new ArrayList();
Point p; // Allocate a Point (not in the heap).
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i; // Initialize the members in the value type.
a.Add(p); // Box the value type and add the reference to the Arraylist.
}
以下是對所發生情況的一般描述:
Point值類型必須轉換為真正的堆管理object,並且必須獲得對這個object的引用。在運行時,將當前駐留在Point值類型實例p中的字段復制到新分配的Point ZA8CFDE6331BD49EB2AC96F89111666 裝箱點 object(現在是引用類型)的地址被返回,然后傳遞給 Add 方法。 點 object 將保留在堆中,直到它被垃圾回收。
所以我的假設是,當編譯器編譯代碼並檢測到需要裝箱時,它會生成 IL 代碼(為便於閱讀,采用 C# 格式):
ArrayList a = new ArrayList();
Point p;
for (Int32 i = 0; i < 10; i++) {
p.x = p.y = i;
Wrapper w = new Wrapper(p); // Wrapper w is a dynamic generate class instance that takes a Point struct instance to copy its fields internally, I know it is not the exact correct format to express, but you get my idea.
a.Add(w);
}
Q1-我的假設正確嗎?
Q2-如果我的假設是正確的,這意味着p
仍然存在於堆棧中,我們可以將p
作為結構實例重用,這會稍微影響性能,因為p
仍然存在於堆棧中,非常多余,但由於堆棧是一個短壽命數據結構快速展開,所以我們並不真正關心冗余結構實例,因為影響是如此之小以至於不需要考慮它?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.