简体   繁体   中英

System.Type; implicit cast to string

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM