声明在C#中的一长串单行线

声明在C#中的一长串单行线

本文介绍了声明在C#中的一长串单行线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有申报在C#长的单行串一个体面的方式,例如,它不是不可能的声明和/或查看字符串在编辑器?

Is there a decent way to declare a long single line string in C#, such that it isn't impossible to declare and/or view the string in an editor?

我知道的选项是:

1:让它运行。这是不好的,因为,因为你的字符串落后的方式关闭屏幕的右侧,使得开发者阅读邮件有恼人的滚动和阅读。

1: Let it run. This is bad because because your string trails way off to the right of the screen, making a developer reading the message have to annoying scroll and read.

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:@ +换行。这看起来代码不错,但引入了新行的字符串。此外,如果你希望它看起来代码不错,你不仅得到换行,但你也字符串的每一行的开头得到尴尬的空间。

2: @+newlines. This looks nice in code, but introduces newlines to the string. Furthermore, if you want it to look nice in code, not only do you get newlines, but you also get awkward spaces at the beginning of each line of the string.

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:+ ... 这工作得很好,但令人沮丧的超级输入。如果我需要添加一行文本的价值一半的地方我必须更新各类+的一切围绕移动文本。

3: "" + ... This works fine, but is super frustrating to type. If I need to add half a line's worth of text somewhere I have to update all kinds of +'s and move text all around.

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或string.concat 。如上述基本相同,但没有加号。有同样的好处和缺点。

4: string.format or string.concat. Basically the same as above, but without the plus signs. Has the same benefits and downsides.

难道真的没有办法做到这一点呢?

Is there really no way to do this well?

推荐答案

这取决于字符串将如何收场被使用。这里所有的答案都有效,但环境是很重要的。如果长字符串s将被记录下来,它应该有一个记录后卫测试所包围,比如这个log4net的例子:

It depends on how the string is going to wind up being used. All the answers here are valid, but context is important. If long string "s" is going to be logged, it should be surrounded with a logging guard test, such as this Log4net example:

if (log.IsDebug) {
    string s = "blah blah blah" +
    // whatever concatenation you think looks the best can be used here,
    // since it's guarded...
}

如果s被准备的长串将被显示给用户,随后的开发艺术的的回答是最好的选择......这些应该是资源文件。

If the long string s is going to be displayed to a user, then Developer Art's answer is the best choice...those should be in resource file.

有关的其他用途(生成SQL查询字符串,写入文件,[但再考虑资源对这些],等...),你在哪里串联不仅仅是文字多,考虑的StringBuilder作为的瓦埃勒Dalloul 的建议,特别是如果你的字符串有可能会风在一个函数才可能,在遥远未来的某个日期,堪称一个时间关键型应用(所有这些调用加起来)很多很多次。我这样做,例如,建立一个SQL查询,在那里我有一个是变量参数时。

For other uses (generating SQL query strings, writing to files [but consider resources again for these], etc...), where you are concatenating more than just literals, consider StringBuilder as Wael Dalloul suggests, especially if your string might possibly wind up in a function that just may, at some date in the distant future, be called many many times in a time-critical application (All those invocations add up). I do this, for example, when building a SQL query where I have parameters that are variables.

除此之外,没有,我不知道的东西,这两个看起来很漂亮,很容易输入(尽管换行的建议是一个不错的主意,它可能不能很好的转化为比较工具,代码打印出来,或代码审查工具)。这些都是在休息。 (我个人使用的加号的方式,使线包裹整齐的为我们的打印输出和代码审查)。

Other than that, no, I don't know of anything that both looks pretty and is easy to type (though the word wrap suggestion is a nice idea, it may not translate well to diff tools, code print outs, or code review tools). Those are the breaks. (I personally use the plus-sign approach to make the line-wraps neat for our print outs and code reviews).

这篇关于声明在C#中的一长串单行线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:40