Why is Environment.NewLine = "\r\n" when "\n" in a string literal functions the same?

Viewed 27054

In C# if you do something like string newLine = Environment.NewLine; and inspect the value of newLine you'll find that it is "\r\n". However, if I do something like;

  string[] test = new string[] { "one", "two", "three" };

  Console.WriteLine("With plain slash n:");
  Console.Write(String.Join("\n", test));
  Console.WriteLine("\nWith Environment.NewLine:");
  Console.Write(String.Join(Environment.NewLine, test));

The output is functionally the same.

Why is Environment.NewLine set to \r\n instead of just \n? Doesn't that just make it more difficult to convert from string to character array? Also I read something which led me to believe some input fields don't count Environment.NewLine as two characters which could cause problems if your backing fields are restricted to the length of the input field. Are there any positive reasons for it? Is it just legacy cruft at this point?

3 Answers
Related