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