3 ms·
Boxing means transforming a variable of a value type into a variable of reference type. //boxing var a = 5; //int, value type string b = (string)a; //string,
by DeathArrow 2y ago
Boxing means transforming a variable of a value type into a variable of reference type.
//boxing
var a = 5; //int, value type
string b = (string)a; //string, reference type
Unboxing is the reverse process of transforming variables of reference types to value types.
A value type variable holds the value directly, while a reference type variable holds a reference that points to a memory address where the actual value is found.
- kragen 2y agoThis is true in some circumstances but not in general. Consider how integers are represented in Emacs Lisp, OCaml, or Smalltalk. They're unboxed, but boxing them like CPython does wouldn't change their semantics, so it doesn't seem correct to say that it would change their type. And there's no way in the language to box them. Fundamentally, boxing is a question about implementation, not semantics. I think a more generally correct statement would be: > An unboxed value can be held in CPU registers directly, while a boxed value is referenced through a memory address where the actual value is found.