logo search
Учебник_ПОА

Boxing and Unboxing

Boxing is name given to the process whereby a value type is converted into a reference type. When you box a variable, you are creating a reference variable that points to a new copy on the heap. You will encounter unboxing when you use classes designed for use with objects: for example, using an ArrayList to store integers. When you store an integer in the ArrayList, it's boxed. When you retrieve an integer, it must be unboxed.

System.Collections.ArrayList list =

new System.Collections.ArrayList(); // list is a reference type

int n = 67; // n is a value type

list.Add(n); // n is boxed

n = (int)list[0]; // list[0] is unboxed