Home
VB.NET
Environment.NewLine, vbCrLf
This page was last reviewed on Dec 7, 2023.
Dot Net Perls
Environment.NewLine equals \r\n. In the VB.NET programming language, you can access the Environment.NewLine property. This leads to clear and readable code.
Property
Special constant. In this language, we can alternatively use the vbCrLf value directly. This value helps with compatibility with older programs. We review this useful property.
StringBuilder Append
Example. First, we will look at the Environment.NewLine property. 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.
Module Module1 Sub Main() ' Create two-line string. Dim value As String = "[First" + _ Environment.NewLine + _ "Second]" ' Write to console. Console.WriteLine(value) End Sub End Module
[First Second]
Example 2. This is another way you can insert a newline. 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 concat operator on the string literal. Its effect is equivalent to the + operator shown in the first example.
Module Module1 Sub Main() ' Use vbCrLf. Dim value As String = "[First" & vbCrLf & "Second]" ' Write to console. Console.WriteLine(value) End Sub End Module
[First Second]
IL. You are probably curious what the intermediate language of this program looks like. The vbCrLf is the same as "\r\n" in the C# language.
Info 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".
L_0001: ldstr "[First\r\nSecond]"
Summary. Depending on your preferences, you can either use Environment.NewLine or vbCrLf. For many programmers, Environment.NewLine is easier to read, so should be preferred.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.