0

We are facing issue when defining verbatim multiline strings (@"some string") in Visual Studio 2022. Although we are compling code on Windows machine and variable Environment.NewLine defines \r\n as new line, verbatim multiline string contains just \n. See below.

MissingCariageReturn

Does anybody have idea why carriage return is missing in string?

Pavel Cermak
  • 1,215
  • 10
  • 13
  • 3
    What does your source file have as the line endings for that part of the code? I suggest you examine it in a binary file editor. `Environment.NewLine` is irrelevant. – Jon Skeet Mar 29 '22 at 15:27
  • 3
    Soo... That's XML - why are you worried about whitespace? – Caius Jard Mar 29 '22 at 15:33
  • In Windows `\r` is optional. When you go to next line, by default the cursor will point to the beginning of the line. – Mohammad Mirmostafa Mar 29 '22 at 19:38
  • The thing is that we have written unit tests, which compares generated XML against the predefined value. Unfortunately it passes on some workstations (which are generating `\r\n` and it fails on some workstations (as on mine where the line break is defined just only as `\n` – Pavel Cermak Mar 29 '22 at 20:16
  • 1
    Bear in mind that there's likely a difference between newlines in .Net and whatever environment it's running under Windows/MacOS/Linux. – phuzi Mar 30 '22 at 09:02

1 Answers1

2

Finally found answer, thanks to John Skeet who gave me a hint.

Behaviour was caused by the code editor - in our case Visual Studio 2022. Basically each code editor allows you to define line breaks behaviour and you can override it by settings in .editorconfig - property end_of_line - see documentation

Here is an example of the .editorConfig file:

# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
[*.cs]
end_of_line = crlf

You can also define behaviour for files in Git repository using .gitattributes file:

*.cs text eol=crlf

Visual Studio also allows you to reformat current file using option Edit->Advanced->Set End of Line Sequence but this is just applied to currently opened file. enter image description here

Pavel Cermak
  • 1,215
  • 10
  • 13