C# - multiline verbatim string does not contains carriage return (\r) for new lines in Visual Studion 2022

301 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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