VB.NET Environment.NewLine: vbCrLfThese VB.NET programs use the Environment.NewLine property. NewLine equals \r\n.
Environment.NewLine equals \r\n. In the VB.NET programming language, you can access the Environment.NewLine property. You can alternatively use the vbCrLf value directly. We review this property.
First, we will look at the Environment.NewLine property in a VB.NET program. This property simply returns the string literal equal to vbCrLf, which is equal to "\r\n" in C# and other languages.
Tip: For some programmers, using Environment.NewLine is more descriptive, in that it indicates exactly what it is.
Info: In most cases, there is no functional difference between Environment.NewLine and alternative representations.
VB.NET program that uses Environment.NewLine
' Create two-line string.
Dim value As String = "[First" + _
Environment.NewLine + _
' Write to console.
This is another way you can insert a newline into your VB.NET strings. The vbCrLf is a special symbol that is compiled into the string literal "\r\n" as represented in other languages such as the C# language.
Here: We use the & operator on the string literal. Its effect is equivalent to the + operator shown in the first example.
VB.NET program that uses vbCrLf
' Use vbCrLf.
Dim value As String = "[First" & vbCrLf & "Second]"
' Write to console.
You are probably curious what the intermediate language of this program looks like. The vcCrLf is the same as "\r\n" in the C# language. At compile-time, the complete string literal is created and used directly in the program.
Next: This is the relevant instruction from the intermediate language: The string is "\r\n".
IL for vbCrLf
L_0001: ldstr "[First\r\nSecond]"
We dived into newlines. Depending on your preferences, you can either use Environment.NewLine or vbCrLf. For many programmers, the vbCrLf name may seem like Hungarian notation. It does not have full words but instead uses abbreviations.
Note: These developers would be wise to choose Environment.NewLine to preserve their sanity.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.