2

I was trying to write out a multi-line C# string literal to a file and noticed that the line endings of the string literal is always CRLF.

To give more context: I am using .NET Core 2.1 and I am building and running this sample app in Linux.

Note I am not using Git so this is not related to Git line ending handling.

Is this expected? I was hoping that the line endings would actually be LF and not CRLF.

Repro code:

   class Program
    {
        const string script =
        @"#!/bin/bash
echo Hello
echo Hi
echo Hey
";

        static void Main(string[] args)
        {
            string text;
            if (args.Length > 0)
            {
                // This gets written as CRLF
                text = script;
            }
            else
            {
                // This gets written as LF(probably because StringBuilder figures
                // in which OS its running and does the right thing)
                var sb = new StringBuilder();
                sb.AppendLine("#!/bin/bash");
                sb.AppendLine("echo Hello");
                sb.AppendLine("echo Hi");
                sb.AppendLine("echo Hey");
                text = sb.ToString();
            }

            var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString("N")}.sh");
            File.WriteAllText(path, text);
            Console.WriteLine($"Text written to {path}");
        }
    }

Update

Just FYI for anyone who is interested...I posted a question about this over here: https://github.com/dotnet/csharplang/issues/1877

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • I guess `\r\n` is hardcoded in the compiler. but need confirmation. – Selman Genç Sep 21 '18 at 20:50
  • 1
    Possible duplicate of [C# Verbatim String Line Breaks: CRLF, CR, or LF?](https://stackoverflow.com/questions/48196840/c-sharp-verbatim-string-line-breaks-crlf-cr-or-lf) – mjwills Sep 21 '18 at 22:33
  • 1
    `Also this is not a duplicate` The duplicate states `Since no exception is made for line endings, you get whatever line endings were used in the source file. As you found out.` That feels quite relevant to your problem. How did you come to the conclusion that it is not relevant? – mjwills Sep 22 '18 at 00:09

3 Answers3

0

I posted an issue related to this over here https://github.com/dotnet/csharplang/issues/1877 and it appears this is an expected behavior.

The above issue talks about using another mechanism which understands writing line endings as here: Writing Unix style text file in C#

However as noted here by me, it does not work for C# multi-line strings, so I had to resort to using Replace("\r\n", "\n") instead in my case.

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • 1
    That is completely consistent with the duplicate I posted. If the source code has crlf then you are seeing expected behaviour. – mjwills Sep 22 '18 at 02:39
  • @mjwills: This is not related to Git and to be clear I am not even using a version control system. This is how the C# compiler works. – Kiran Sep 22 '18 at 07:17
0

The answer is the same as https://stackoverflow.com/a/48196962/771768

If your C# source file has CRLF ending, the multiline string will have CRLF.

If the source file has LF endings, the string will have LF.

If your source file has mixed LF and CRLF, then the string will have mixed LF and CRLF endings. But other devs will rightfully complain that this will be confusing.

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
-4

You should use Environment.NewLine instead.

Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18