What is Boxing and Unboxing?
What is Boxing and Unboxing?
Boxing and unboxing are two related concepts in C# that allow the conversion between value types and reference types. Here's an explanation of each:
Boxing is the process of converting a value type to a reference type. When a value type is boxed, it is converted to an object type and stored on the managed heap. This allows the value type to be treated as an object, which means it can be passed as a parameter to methods that expect an object, stored in collections that store objects, or used with other reference types.
Here's an example of boxing:
int i = 42;
object obj = i;
In this example, the integer value 42
is boxed into an object
reference. The actual integer value is copied into a new object on the managed heap.
Unboxing is the reverse process of boxing. It involves converting a reference type back to a value type. To u...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào