0

我重写了我的 ToString() 方法,我的类包含许多字段。
我只能在最后一个字符串之后有新行,并且只能将变量放在下一行。
注意 - 这仅用于视觉目的,我不希望在打印之间分隔一行.. 只在我的 21" 屏幕上看到整行而不滚动。

这有效:

    public override string ToString()
    {
        return String.Format("{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4}, Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
            base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
    }

但这不起作用(相同的代码,仅在第 4 个参数后按回车键):

    public override string ToString()
    {
        return String.Format("{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
        Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
            base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
    }
4

4 回答 4

2

String是引号之间的一行中的一系列符号。

"one two" - is string.
"one
  two" - is an error.

您可以通过连接有效字符串在 C# 中创建多行字符串:

String s = "one" + Environment.NewLine + 
           " two";

或通过@在字符串之前添加

String s = @"one
             two";
于 2013-09-10T09:14:07.967 回答
1

使用以 @ 字符开头的逐字字符串:

public override string ToString()
{
    return String.Format(@"{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
    Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
        base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
于 2013-09-10T09:14:35.437 回答
0

我唯一能想到的如下:

public override string ToString()
{
    return String.Format(
        "{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3},"
        + " Model = {4}, Firmware = {5}, Driver = {6}, MacAddress = {7},"
        + " Scenes count = {8}",
        base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
于 2013-09-10T09:12:44.843 回答
0

尝试这个:

string temp_var = @"{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
          Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}";
temp_var = string.Format(base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
return temp_var;

只需将值分配给字符串变量..

于 2013-09-10T09:23:56.920 回答