在查看代码定义窗口下的System.Type类时,我似乎无法理解如何将此类的实例隐式转换为字符串。 例如,在以下代码中:
int foo = 0; Console.WriteLine("Hi! I'm a type of type {0}", foo.GetType());如何将GetType()产生的System.Type隐式转换为字符串?
While looking at the System.Type class under the Code Definition Window, I cannot seem to understand how an instance of this class is implicitly cast to string. For example, on the following code:
int foo = 0; Console.WriteLine("Hi! I'm a type of type {0}", foo.GetType());How was the System.Type resulting from GetType() implicitly cast to string?
最满意答案
它不是隐式地转换为字符串。 它被转换为Object (使用标准的隐式引用转换),并且String.Format正确地格式化它 - 在这种情况下通过调用ToString 。
这不是在编译时完成的。 您显示的代码调用Console.WriteLine(string, object)重载。
It's not being implicitly cast to string. It's being converted to Object (with the standard implicit reference conversion), and String.Format is formatting it appropriately - by calling ToString in this case.
This is not being done at compile-time. The code you've shown calls the Console.WriteLine(string, object) overload.
更多推荐
发布评论