我遇到了一种奇怪的实现方式ToString()
,我想知道它是如何工作的:
public string tostr(int n)
{
string s = "";
foreach (char c in n-- + "") { //<------HOW IS THIS POSSIBLE ?
s = s + c;
}
return s;
}
Run Code Online (Sandbox Code Playgroud)
迭代器是否假定大小为char
?
我有一个这样的方法:void m1(string str)
并有一个这样的类:
public class MyClass
{
public bool b1 { set; get; }
//and other properties
}
Run Code Online (Sandbox Code Playgroud)
现在为什么下面的代码不会导致编译错误?
IClass2 _class2 = new Class2();
MyClass c1 = new MyClass();
_class2.m1("abcdef" + c1);
Run Code Online (Sandbox Code Playgroud)
当我调试它时,我意识到c1.ToString()
已经传递给了m1
.为什么会.ToString()
发生这种自动?我唯一可以说的是m1已经在IClass2
接口中定义并且已经实现了Class2
.