In .NET I can provide both \r or \n string literals, but there is a way to insert
something like "new line" special character like Environment.NewLine static property?
12 Answers
Well, simple options are:
string.Format:string x = string.Format("first line{0}second line", Environment.NewLine);String concatenation:
string x = "first line" + Environment.NewLine + "second line";String interpolation (in C#6 and above):
string x = $"first line{Environment.NewLine}second line";
You could also use \n everywhere, and replace:
string x = "first line\nsecond line\nthird line".Replace("\n",
Environment.NewLine);
Note that you can't make this a string constant, because the value of Environment.NewLine will only be available at execution time.
6 Comments
Environment.NewLine? Quite the contrary, it's a good practice to use itIf you want a const string that contains Environment.NewLine in it you can do something like this:
const string stringWithNewLine =
@"first line
second line
third line";
EDIT
Since this is in a const string it is done in compile time therefore it is the compiler's interpretation of a newline. I can't seem to find a reference explaining this behavior but, I can prove it works as intended. I compiled this code on both Windows and Ubuntu (with Mono) then disassembled and these are the results:

As you can see, in Windows newlines are interpreted as \r\n and on Ubuntu as \n
4 Comments
var sb = new StringBuilder();
sb.Append(first);
sb.AppendLine(); // which is equal to Append(Environment.NewLine);
sb.Append(second);
return sb.ToString();
7 Comments
first + Environment.NewLine + second which is more efficient and (IMO) easier to read?String.Format will produce 1 string at once (but it's internally a bit slow because of culture specific concatenations, etc), while string concatenation - 1 resulting + 1 temporary, right?String.Concatenate, which directly builds just one output string (IIRC, if the strings are literals the concatenation is done in the compiler."a"+b+"c"+d, etc) by performance are equal to a single one? Or just converted to String.Concatenate(a,b,c,d,etc), right?string.Format in the comment. The string concatenation won't produce any temporary strings, because the compiler will call string.Concat(first, Environment.NewLine, second).One more way of convenient placement of Environment.NewLine in format string. The idea is to create string extension method that formats string as usual but also replaces {nl} in text with Environment.NewLine
Usage
" X={0} {nl} Y={1}{nl} X+Y={2}".FormatIt(1, 2, 1+2);
gives:
X=1
Y=2
X+Y=3
Code
///<summary>
/// Use "string".FormatIt(...) instead of string.Format("string, ...)
/// Use {nl} in text to insert Environment.NewLine
///</summary>
///<exception cref="ArgumentNullException">If format is null</exception>
[StringFormatMethod("format")]
public static string FormatIt(this string format, params object[] args)
{
if (format == null) throw new ArgumentNullException("format");
return string.Format(format.Replace("{nl}", Environment.NewLine), args);
}
Note
If you want ReSharper to highlight your parameters, add attribute to the method above
[StringFormatMethod("format")]
This implementation is obviously less efficient than just String.Format
Maybe one, who interested in this question would be interested in the next question too: Named string formatting in C#
Comments
If you really want the New Line string as a constant, then you can do this:
public readonly string myVar = Environment.NewLine;
The user of the readonly keyword in C# means that this variable can only be assigned to once. You can find the documentation on it here. It allows the declaration of a constant variable whose value isn't known until execution time.
Comments
static class MyClass
{
public const string NewLine="\n";
}
string x = "first line" + MyClass.NewLine + "second line"
3 Comments
Environment.NewLine -- see the other answers.const stringnewer .net versions allow you to use $ in front of the literal which allows you to use variables inside like follows:
var x = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
3 Comments
If I understand the question: Couple "\r\n" to get that new line below in a textbox. My example worked -
string s1 = comboBox1.Text; // s1 is the variable assigned to box 1, etc.
string s2 = comboBox2.Text;
string both = s1 + "\r\n" + s2;
textBox1.Text = both;
A typical answer could be s1
s2 in the text box using defined type style.