Possible Duplicate:
How should I concatenate strings?
当性能不是important时,有几种方法可以在日常任务中连接字符串.
result = a + ":"+ b
result = string.Concat(a, ":", c)
result = string.Format("{0}:{1}", a, b);
StringBuilder 方法
- ... ?
您更喜欢什么?如果效率不重要但您希望保持代码的可读性最符合您的口味,那为什么?
这取决于用途。当您只想连接两个字符串时,使用 a + b
比 string.Format("{0}{1}", a, b)
更具可读性.但是,它变得越来越复杂,我更喜欢使用 string.Format
。比较一下:
string x = string.Format("-{0}- ({1})", a, b);
反对:
string x = "-" + a + "- (" + b + ")";
我认为在大多数情况下,很容易找到最易读的做事方式。在争论哪一个更具可读性的情况下,只需选择一个,因为你的老板不会为这些毫无意义的讨论买单;-)