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
  • 55,421
  • 15
  • 173
  • 154
  • I guess `\r\n` is hardcoded in the compiler. but need confirmation. – Selman Genç Sep 21 '18 at 20:50
  • 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
  • `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

2 Answers2

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
  • 55,421
  • 15
  • 173
  • 154
  • 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
-4

You should use Environment.NewLine instead.

Mauricio Atanache
  • 2,049
  • 1
  • 12
  • 17