tags:

views:

88

answers:

3

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
ints get boxed too.
Brian Rasmussen
@Brian: int is value type
TcKs
Yeah, but it is not a struct.
Brian Rasmussen
@Brian Rasmussen: actually they are. (See comments in answer) http://stackoverflow.com/questions/2083012/reflection-has-isclass-but-no-isstruct
acidzombie24
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
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
@Brian: After reading that last comment i learned something too. Interesting.
acidzombie24
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
+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
+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