相关疑难解决方法(0)

int + string如何成为字符串?

我遇到了一种奇怪的实现方式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

.net c# obfuscation

28
推荐指数
4
解决办法
3082
查看次数

自动.ToString()?

我有一个这样的方法: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.

.net c# interface tostring

3
推荐指数
2
解决办法
2754
查看次数

标签 统计

.net ×2

c# ×2

interface ×1

obfuscation ×1

tostring ×1