[英]When using object type stores an int value, why the object.GetType() returns system.Int32?
The code like below:如下代码:
object i = 11;
Console.WriteLine(i.GetType());
It prints out "System.Int32", why?它打印出“System.Int32”,为什么? I thought it should be "system.object".
我认为它应该是“system.object”。
GetType()
returns the actual type of the object that the value refers to. GetType()
返回该值所指的 object 的实际类型。 It doesn't return the type of the variable used to access the object.它不返回用于访问 object 的变量类型。 So as a simpler example:
所以作为一个更简单的例子:
object x = "this is a string";
Console.WriteLine(x.GetType()); // Prints System.String
In this case, the type is "boxed System.Int32
" - but most of .NET doesn't differentiate between "a regular value type" and "the boxed type equivalent to the value type", including GetType()
.在这种情况下,类型是“boxed
System.Int32
” - 但 .NET 的大部分内容都没有区分“常规值类型”和“与值类型等效的盒装类型”,包括GetType()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.