dotnetfiddle.net produces newline as \n instead of \r\n in some cases

155 Views Asked by At

While testing some snippets in dotnetfiddle.net I noticed strange phenomenom.

If verbatim string literal (string starts @") contains newline in it, compiler emits newline as \n in output string instead of \r\n. (for example Environment.NewLine produces \r\n in all cases).

Example code below can also be found in https://dotnetfiddle.net/53qGTM:

string verbatimString = @"L1
L2";

string normalString = @"L1" + Environment.NewLine + "L2";

Console.WriteLine("=====Verbatim=====");
foreach (char c in verbatimString)
{
    Console.WriteLine("ascii:" + (int)c + ", char:" + c);
}

Console.WriteLine("=====Normal=====");
foreach (char c in normalString)
{
    Console.WriteLine("ascii:" + (int)c + ", char:" + c);
}

output:

=====Verbatim=====
ascii:76, char:L
ascii:49, char:1
ascii:10, char:

ascii:76, char:L
ascii:50, char:2
=====Normal=====
ascii:76, char:L
ascii:49, char:1
ascii:13, char: 
ascii:10, char:

ascii:76, char:L
ascii:50, char:2

When compiling same code in Visual Studio, newline is correctly \r\n in both cases. It seems as bug in dotnetfiddle-compiler, is it so?

While making some investigations I also made an assumption what's going on and I'll also make first answer to this question as recommended.

1

There are 1 best solutions below

0
On BEST ANSWER

dotnetfiddle.net executes code compiling and execution in backend. When running code snippet, code editor web view sends code to backend as json and receives output as json.

As part of my code execution, following json string is sent (grabbed in chrome dev tools):

"CodeBlock": "using System;\n\npublic class Program\n{\n\tpublic static void Main()\n\t{\n\t\tstring verbatimString = @\"L1\nL2\";\n\t\tstring normalString = @\"L1\" + Environment.NewLine + \"L2\";\n\t\tConsole.WriteLine(\"=====Verbatim=====\");\n\t\tforeach (char c in verbatimString)\n\t\t{\n\t\t\tConsole.WriteLine(\"ascii:\" + (int)c + \", char:\" + c);\n\t\t}\n\n\t\tConsole.WriteLine(\"=====Normal=====\");\n\t\tforeach (char c in normalString)\n\t\t{\n\t\t\tConsole.WriteLine(\"ascii:\" + (int)c + \", char:\" + c);\n\t\t}\n\t}\n}"

In part of json, my verbatingString-variable definition is following: string verbatimString = @\"L1\nL2\";

There is standard json newline char \n embedded in my string literal. Seems that dotnetfiddle backend does not any conversion on given string literals. It takes string literals literally (pun intended).