BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Environment.NewLine

When working with strings that contain multiple lines of text, each line is separated with a line break. Line breaks are often added by inserting carriage return and line feed escape characters. This can cause cross-platform compatibility problems.

Line Breaks

It is common to wish to add line breaks to text held in a string or used for output purposes. In a Microsoft Windows environment a standard line break consists of two escape characters. The first is a carriage return and the second a line feed. In a C# string, these can be represented as "\r\n".

When you add a line break to a string, it is not advised that you append the two escape characters manually. This is because software targeting the .NET framework can be executed on non-Windows systems, which may not use the same escape sequence for a line break. For example, Unix systems generally use only a line feed escape character for a line break and do not include the carriage return. Using both characters could therefore cause cross-platform compatibility issues.

To resolve this problem, the .NET framework includes a property that contains the correct escape sequence for a line break. This static property is found in the Environment class in the System namespace. The property returns a line feed string that is appropriate to the current operating system.

string newLine = Environment.NewLine;

NB: Standard .NET methods that generate line breaks, such as Console.WriteLine, use the line break sequence from the Environment.NewLine property.

28 October 2009