![](/img/trans.png)
[英]Removing duplicates using LINQ returns “Error removing duplicates: Unable to cast object of type 'System.Int32' to type 'System.String'.”
[英]When using object type stores an int value, why the object.GetType() returns system.Int32?
如下代码:
object i = 11;
Console.WriteLine(i.GetType());
它打印出“System.Int32”,为什么? 我认为它应该是“system.object”。
GetType()
返回该值所指的 object 的实际类型。 它不返回用于访问 object 的变量类型。 所以作为一个更简单的例子:
object x = "this is a string";
Console.WriteLine(x.GetType()); // Prints System.String
在这种情况下,类型是“boxed System.Int32
” - 但 .NET 的大部分内容都没有区分“常规值类型”和“与值类型等效的盒装类型”,包括GetType()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.