boxing and unboxing, why aren't the outputs both "System.Object"?
Asked Answered
C

5

8

I got the following code:

object var3 = 3;
Console.WriteLine(var3.GetType().ToString());
Console.WriteLine(typeof(object).ToString());

The output is:

System.Int32
System.Object

Why aren't they both System.Object?

Cistaceous answered 10/8, 2010 at 8:50 Comment(2)
@Also OP, Both of the Console.WriteLines would work, even without the explicit ToString() - it would be implicitly called if your argument type is an Object.Hallucinate
removed salutation: "Thanks in advance.", also removed illegal words from title: "Question about ", don't do that next timeCrossbench
H
4

If you're asking why the boxedObject.GetType() does not return Object.. check out the image under the Section 'Boxing Conversion' on the MSDN Boxing and Unboxing page. Good question btw.. atleast my understanding of your question.

Although I may not be technically correct, it looks like

  • When moved to the heap, a new object is created - its Type pointer set to the original value type's Type object (here System.Int32). This explains GetType() (and also the error if you try to unbox it to a different type).
  • The actual value is then copied over into this object.
Hallucinate answered 10/8, 2010 at 9:10 Comment(0)
N
7

The GetType() function returns the actual type of the instance in the variable.

Even though your variable is declared as object, it's actually holding a boxed Int32 instance.

Nerynesbit answered 10/8, 2010 at 8:51 Comment(3)
I have no idea what the other answers are trying to prove :)Egon
I think his question is why does the box's GetType (the object) return Int32 (the type of the boxed value) instead of Object - since boxing causes it to create an object on the heap. If not you're getting a +1Hallucinate
@leppie: There sure are a lot of them, aren't there :)Somnifacient
H
4

If you're asking why the boxedObject.GetType() does not return Object.. check out the image under the Section 'Boxing Conversion' on the MSDN Boxing and Unboxing page. Good question btw.. atleast my understanding of your question.

Although I may not be technically correct, it looks like

  • When moved to the heap, a new object is created - its Type pointer set to the original value type's Type object (here System.Int32). This explains GetType() (and also the error if you try to unbox it to a different type).
  • The actual value is then copied over into this object.
Hallucinate answered 10/8, 2010 at 9:10 Comment(0)
S
3

Ignoring the topic of boxing, all classes inherit from type object. This is true for both reference types and value types. GetType shows the most derived type, which in this case is System.Int32.

One of the few times GetType is going to return System.Object is if you do this:

object var = new Object();
Console.WriteLine(var.GetType().ToString());

Boxing refers to when a value type is pointed to by a reference type. Generally this is done as a System.Object reference. TypeOf will return the most derived actual type, not the reference type.

class A
{
}

class B : A
{
}

class C : B
{
}

object obj1 = new ClassA();
ClassB obj2 = new ClassB();
ClassB obj3 = new ClassC();

GetType will do similar things for these types.

System.Console.WriteLine(obj1.GetType().ToString());
System.Console.WriteLine(obj2.GetType().ToString());
System.Console.WriteLine(obj3.GetType().ToString());

ClassA
ClassB
ClassC

Somnifacient answered 10/8, 2010 at 8:53 Comment(1)
Value types don't really derive from Object. They're completely outside the type hierarchy. For every value type, however, there is a class type which derives from the ironically-named ValueType (despite its name, it's a class type). The system defines implicit bidirectional conversion operators between each value type and its corresponding auto-generated class type. The implicit conversion from a value type to a class type is called "boxing"; the reverse conversion is "unboxing".Cordelier
Q
1

This isn't really about boxing; this is about the behaviour of GetType. It returns the type of the value of the variable, not the type the variable was declared with:

    object var4 = new List<string>();
    Console.WriteLine(var4.GetType().ToString());

won't return System.Object either.

Quasi answered 10/8, 2010 at 8:56 Comment(1)
It will return the type of the instance, not the type of the referenceSomnifacient
S
0

declarations of variable is compile time only information whereas method execution is runtime. In other words there's no way GetType() can know what type the object was declared as it can only know the actual type of the object at runtime.

similar if you had

class a
{
}

class b : a

a bInstance = new b();
bInstance.GetType();

the call to bInstance.GetType() have no way of knowing that the variable was declared as type 'a' and I don't think you expect it to return 'a' in this case either. However in the example above a is my abbreviation of object and b is for System.Int32

Saddler answered 10/8, 2010 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.