I was wondering, does a class get boxed? I always assumed every class had a virtual table which can be used to identify the class, so does it need to be boxed?
+3
A:
Only value types (structs) get boxed. Class instances do not get boxed.
itowlson
2010-01-28 08:38:43
ints get boxed too.
Brian Rasmussen
2010-01-28 08:39:15
@Brian: int is value type
TcKs
2010-01-28 08:40:26
Yeah, but it is not a struct.
Brian Rasmussen
2010-01-28 08:42:05
@Brian Rasmussen: actually they are. (See comments in answer) http://stackoverflow.com/questions/2083012/reflection-has-isclass-but-no-isstruct
acidzombie24
2010-01-28 08:43:20
Not according to the language specification: "C#’s value types are further divided into simple types, enum types, struct types, and nullable types." and int, short, long etc are listed as simple types.
Brian Rasmussen
2010-01-28 08:48:29
Okay, I learned something today: The language specification also points out "C# provides a set of predefined struct types called the simple types".
Brian Rasmussen
2010-01-28 08:50:58
@Brian: After reading that last comment i learned something too. Interesting.
acidzombie24
2010-01-28 09:11:22
Anything that is derived from "ValueType" class can be boxed. Interestingly int or other value type definition does not seem to be inheriting from this class but it provide a common base class to identify all value types.
affan
2010-01-28 09:48:55
+3
A:
No. Classes are reference types so no need for boxing. Boxing is used to represent values as objects (in order to provide .NET's unified type system). As instances of classes are already objects they never need to be boxed.
Brian Rasmussen
2010-01-28 08:38:46
+1
A:
No they are not.
Boxing referes to a primite type (int, char, long etc...) being wrapped into a class (i.e. boxed).
Oded
2010-01-28 08:39:26