Boxing
Everything in C# is an object (caveats apply), meaning that they derive from the System.Object class. Even primitive data types such as int, float, and bool are implicitly derived from System.Object, which is itself a reference type. This is a special case, which allows them access to helper methods such as ToString() so that they can customize their string representation, but without actually turning them into reference types. Whenever one of these value types is implicitly treated in such a way that it must act as an object, the CLR automatically creates a temporary object to store, or box, the value inside so that it can be treated as a typical reference type object. As we should expect, this results in a heap allocation to create ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access