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

前端 未结 5 1439
情书的邮戳
情书的邮戳 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:43

    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();
        Console.WriteLine(var4.GetType().ToString());
    

    won't return System.Object either.

提交回复
热议问题