C# line return character

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

In my C# windows form project, how to use the line return character in a
string so the string will go to a new line?
Thanks for help.


Jason
 
In my C# windows form project, how to use the line return character in a
string so the string will go to a new line?

string s = "First line" + Environment.NewLine + "Second line";


Mattias
 
Jason Huang said:
In my C# windows form project, how to use the line return character in a
string so the string will go to a new line?
Thanks for help.

string s = "hello" + Environment.NewLine + "world.";
 
Mattias said:
string s = "First line" + Environment.NewLine + "Second line";

Doesn't C# treat the "\r\n" as an alias for Environment.NewLine?
Wouldn't the code be more readable as:

string s = "First line\r\nSecondLine";

Just wondering,

Chris
 
Doesn't C# treat the "\r\n" as an alias for Environment.NewLine?

It does in a Windows Environment, but not necessarily.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

Mattias said:
string s = "First line" + Environment.NewLine + "Second line";

Doesn't C# treat the "\r\n" as an alias for Environment.NewLine?
Wouldn't the code be more readable as:

string s = "First line\r\nSecondLine";

Just wondering,

Chris
 
Chris Dunaway said:
Doesn't C# treat the "\r\n" as an alias for Environment.NewLine?
Wouldn't the code be more readable as:

string s = "First line\r\nSecondLine";

Environment.NewLine ends up as "\r\n" on Windows, but won't on other
platforms.

Whether you should use "\r\n" or Environment.NewLine depends on the
context - it's not always a straightforward choice.
 
Back
Top