Quote:
Originally Posted by pegasus001
I don`t quite agree. Because every type in c# inherits the Object class, so how ca you say that they aren`t boxed. They have methods or you forgot :
So this means that they are inside(boxed into) something.
And also C# has no primitive types, because it is oop, it only has predefined types.
|
No not really. Structs inherit from System.ValueType (which does inherit from System.Object but thats irrelevent). There is a huge difference between stucts and classes in C# (it has nothing to do with default member visability like C++). When you create an instance of a struct it is stack allocated and the variable holding it is actually the value. An instance of a class is heap allocated and the variable references(or has the address of) the instance of it. Struct types allow you to have methods like ToString or whatever but that has nothing to do with the fact thats its heap or stack allocated.
If you box a struct (or a primative -same thing) then it gets put on the heap and you have a variable that holds the address of it. This is what boxing really is as you are putting your value type and shoving it into an Object that is on the heap.
What you imply is that when you call a method a something which is a struct type it gets boxed into an object on the heap and then called. This would be a huge waste of resources and is not the case. Boxing does not mean that it is inside something at all. Boxing referes to a value type that has been put on the heap inside an Object.