boxing and unboxing, why aren't the outputs both “System.Object”?

前端 未结 5 1457
情书的邮戳
情书的邮戳 2021-02-15 15:45

I got the following code:

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

The outp

5条回答
  •  庸人自扰
    2021-02-15 16:33

    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.

提交回复
热议问题