object o;
o.ToString()
和 (string) o
有区别吗?
最佳答案
有区别,是的。每个对象都有一个 ToString
方法,但不是每个对象都可以转换为字符串。
int i = 10;
string s1 = i.ToString(); // OK
string s2 = (string)i; // Compile error.
object o = 10;
string s3 = o.ToString(); // OK
string s4 = (string)o; // Runtime error.
关于c# - ToString 方法和转换为字符串之间有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4152205/