我已经为堆栈编写了一个程序。(https://stackoverflow.com/questions/2617367?tab=votes#tab-top)为此,我需要一个 StringBuilder 才能向我展示堆栈中的内容,否则我将获得类名而不是实际值里面。
我的问题是除了 StringBuilder 之外还有其他方法可以解决此类问题吗?
另外在哪些其他情况下会发生这种问题?当我在 1 行上需要几件东西时,我编写 StringBuilder 的方式也感觉很尴尬。
public override string ToString()
{
StringBuilder builder = new StringBuilder();
foreach (int value in tabel)
{
builder.Append(value);
builder.Append(" ");
}
if (tabel.Length == tabel.Length) // this is a bit messy, since I couldn't append after the rest above
{
builder.Append("(top:");
builder.Append(top);
builder.Append(")");
}
return builder.ToString();
}/*ToString*/