在C#中,在多条源代码行之间散布单行字符串文字的最佳方法是什么? [英] In C#, what's the best way to spread a single-line string literal across multiple source lines?

查看:107
本文介绍了在C#中,在多条源代码行之间散布单行字符串文字的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个较长的字符串(> 80个字符),您想跨多个源代码行,但不希望包含任何换行符。

Suppose that you have a lengthy string (> 80 characters) that you want to spread across multiple source lines, but don't want to include any newline characters.

一个选项是连接子字符串:

One option is to concatenate substrings:

string longString = "Lorem ipsum dolor sit amet, consectetur adipisicing" +
    " elit, sed do eiusmod tempor incididunt ut labore et dolore magna" +
    " aliqua. Ut enim ad minim veniam";

有没有更好的方法,还是最好的选择?

Is there a better way, or is this the best option?

编辑:最好是指编码器最容易阅读,编写和编辑。例如,如果您想要想要换行符,则很容易查看:

By "best", I mean easiest for the coder to read, write, and edit. For example, if you did want newlines, it's very easy to look at:

string longString =
@"Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam";

我想知道当您不要想要换行符。

I am wondering if there is something just as clean when you don't want newlines.

推荐答案

我会使用您的方法的一种变体:

I would use a variation of your method:

string longString =
    "Lorem ipsum dolor sit amet, consectetur adipisicing " + 
    "elit, sed do eiusmod tempor incididunt ut labore et dolore magna " + 
    "aliqua. Ut enim ad minim veniam.";

在这里我在等号后的行上开始字符串,以便它们全部对齐,还要确保空格出现在行的末尾(再次,出于对齐目的)。

Here I start the string on the line after the equals sign so that they all line up, and I also make sure the space occurs at the end of the line (again, for alignment purposes).

这篇关于在C#中,在多条源代码行之间散布单行字符串文字的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆