【发布时间】:2010-12-06 15:43:48
【问题描述】:
是否有一种在 C# 中声明长单行字符串的好方法,这样在编辑器中声明和/或查看字符串并非不可能?
我知道的选项是:
1:让它运行。这很糟糕,因为您的字符串拖到屏幕右侧,使阅读消息的开发人员不得不烦人的滚动和阅读。
string s = "this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. ";
2:@+换行符。这在代码中看起来不错,但在字符串中引入了换行符。此外,如果你想让它在代码中看起来不错,你不仅会得到换行符,而且还会在字符串的每一行的开头得到尴尬的空格。
string s = @"this is my really long string. this is my long string.
this line will be indented way too much in the UI.
This line looks silly in code. All of them suffer from newlines in the UI.";
3: "" + ... 这很好用,但是打字非常令人沮丧。如果我需要在某处添加半行的文本,我必须更新各种 + 并移动文本。
string s = "this is my really long string. this is my long string. " +
"this will actually show up properly in the UI and looks " +
"pretty good in the editor, but is just a pain to type out " +
"and maintain";
4:string.format or string.concat。与上面基本相同,但没有加号。具有相同的优点和缺点。
真的没有办法做好吗?
【问题讨论】:
标签: c# string coding-style code-formatting