ToString 和字符串连接 - 意外行为

ToString and string concatenation - unexpected behavior

根据你上网的时候做的

String someString = "" + object1 + object2 + object3;

ToString() 在每个对象上被调用。

但这并没有发生! 此代码:

String a = "a" + foo;
String b = "b" + foo.ToString();

Console.WriteLine(a);
Console.WriteLine(b);

打印:

a
b("key":"foo")

怎么可能?

我对整个项目进行了 Resharper 全面清理,它在某些地方破坏了代码,因为它删除了此类字符串连接中的 ToString()!!浪费了很多时间..

编辑: 这个问题发生在我使用的一个小型图书馆中。我不能提供非常短的单文件代码来重现这个,但我已经用这个库创建了一个小项目并上传到 github:

https://github.com/Vistritium/ToStringCSObjectConcat https://github.com/Vistritium/ToStringCSObjectConcat/blob/master/TestString/Program.cs

库长 1178 行。

我刚刚做了这个,它运行良好

private void button1_Click(object sender, EventArgs e)
{
    var o = new x ();
    string s = "ff" + o;

    Console.WriteLine(s);

}

public class x {

    public override string ToString()
    {
        return "This is string";
    }
}

版画

ffThis is string

这告诉我你做错了什么。我的意思是,你做错的并不像你期望的东西与你的对象在 ToString

中产生的不同

如果您提供了将 class 转换为字符串的 implicit operator,则可能会发生这种情况,例如:

public class Foo
{
    public string Key { get; set; }

    public string Value { get; set; }

    public static implicit operator string(Foo foo)
    {
        return foo == null ? string.Empty : foo.Value;
    }

    public override string ToString()
    {
        var str = string.Empty;
        if (!string.IsNullOrEmpty(Key))
        {
            if (str.Length > 0)
                str += ";";
            str += ("Key=" + Key);
        }
        if (!string.IsNullOrEmpty(Value))
        {
            if (str.Length > 0)
                str += ";";
            str += ("Value=" + Value);
        }
        return str;
    }
}

在那种情况下:

    string a = "a" + new Foo { Key = "foo", Value = "" };
    string b = "b" + new Foo { Key = "foo", Value = "" }.ToString();

    Debug.WriteLine(a); // Prints "a".
    Debug.WriteLine(b); // Prints "bKey=foo

如果 stringFoo.

overloaded the + operator,您也可以获得此效果

更新

来自 C# 语言规范,7.2.2 Operator overloading

All unary and binary operators have predefined implementations that are automatically available in any expression. In addition to the predefined implementations, user-defined implementations can be introduced by including operator declarations in classes and structs (Section 10.9). User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered.

这就是自定义逻辑优先于标准逻辑被调用的原因。