1

In C# define 4 variable as below:

string s1 = "\r"; string s2 = "\n"; string CarriageReturn = (Convert.ToChar(13)).ToString(); string LineFeed = (Convert.ToChar(10)).ToString(); 

Then by watching copy their value in Notepad++ and click on "Show all characters". Interestingly you can see there is no difference between \r and \n and for both of them, it shows CR LF. Is it a bug or something else? How can we explain this?

3
  • 2
    The Windows line ending is CRLF, so I would guess that Notepad++ is automatically doing this to each line. Commented Feb 16, 2018 at 2:41
  • Type into Notepad++ test<enter>again<enter>try<enter> (replacing <enter> with the Enter key on your keyboard) and repeat your Show All Characters. What do you see? Commented Feb 16, 2018 at 2:45
  • How exactly are you getting the values into Notepad++? For instance, are you writing the values to the console and copying them from there? Or, are you watching the values in a debugger window and then copying them to Notepad++? Or are you using another way? The more specific you are, the more likely it is that someone will provide a correct answer. Commented Feb 16, 2018 at 4:43

1 Answer 1

1

Interestingly you can see there no difference with \r and \n and for both of them it shows CR LF Is it a bug or something else?

It is not a bug. CRLF is the default for the Environment.NewLine in Windows: a 'string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.'

How can we explain this?

It probably results from the way you are outputting the string values to a file. If you use a method that adds new lines, such as WriteAllLines() does, then there will automatically be a CRLF at the end of each value you write.

For instance, we can run the following program.

string r = "\r"; string n = "\n"; string CarriageReturn = (Convert.ToChar(13)).ToString(); string LineFeed = (Convert.ToChar(10)).ToString(); var content = new string[] { $"(r:{r})", $"(n:{n})", $"(13:{CarriageReturn})", $"(10:{LineFeed})" }; System.IO.File.WriteAllLines("output1.txt", content); System.IO.File.WriteAllText("output2.txt", string.Join("", content)); 

It produces two output files. The one on the left used WriteAllLines to write four lines. The one on the right used WriteAllText() and did not write any new lines.

enter image description here

In both, all of the content outside parentheses is independent of your code. That is, the CRLF symbols are part of writing a line in the call to WriteAllLines.

Sign up to request clarification or add additional context in comments.

2 Comments

Sounds like they're not even outputting to a file in the first place, just copying the values off of the debugger or something? Your WriteAllLines() example does still write each CR and LF as is.
Hmm, can't be. Copying the values just copies their string representations, not the characters themselves.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.