HomeSearch

VB.NET Environment.NewLine: vbCrLf

These 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.
Example. 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 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 Output [First Second]
Example 2. 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 Module Module1 Sub Main() ' Use vbCrLf. Dim value As String = "[First" & vbCrLf & "Second]" ' Write to console. Console.WriteLine(value) End Sub End Module Output [First Second]
IL. 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]"
Summary. 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 info@dotnetperls.com.
Home
Dot Net Perls