Code Issues – Environment.NewLine can be used
Cause:
When working with strings that contain multiple lines of text, you have to add newline characters to your strings, so each line is separated with a line break. Line breaks are often added by inserting a carriage return line feed escape characters. This can cause cross-platform compatibility issues, because your code might end up being compiled under Mono in Unix, for example.
The “Environment.NewLine can be used” CodeRush code issue shows a suggestion to convert escape characters (“\r\n“) inside your code into the constant value defined in .NET Framework. Changing these strings to the Environment.NewLine constant will, firstly, improve the code clarity, so you don’t have to use escape characters, and, secondly, fix potential platforms portability issues – so, you don’t have to be concerned about such problems.
The Environment.NewLine is a static string property from the System namespace that is tied to the current executing environment or platform. The property returns a line feed string that is appropriate to the current operating system: “\r\n” for non-Unix platforms, or a string containing just a line feed (“\n“) for Unix platforms.
Sample:
How to fix:
- Apply the Use Environment.NewLine refactoring: