2

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 Answers3

3

Only value types (structs) get boxed. Class instances do not get boxed.

itowlson
  • 73,686
  • 17
  • 161
  • 157
  • Yeah, but it is not a struct. – Brian Rasmussen Jan 28 '10 at 08:42
  • 1
    @Brian Rasmussen: actually they are. (See comments in answer) http://stackoverflow.com/questions/2083012/reflection-has-isclass-but-no-isstruct –  Jan 28 '10 at 08:43
  • 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 Jan 28 '10 at 08:48
  • 1
    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 Jan 28 '10 at 08:50
  • @Brian: After reading that last comment i learned something too. Interesting. –  Jan 28 '10 at 09:11
  • 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. – particle Jan 28 '10 at 09:48
3

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
  • 114,645
  • 34
  • 221
  • 317
1

No they are not.

Boxing referes to a primite type (int, char, long etc...) being wrapped into a class (i.e. boxed).

Oded
  • 489,969
  • 99
  • 883
  • 1,009