Adding Linebreaks in C#

Problem:

We want to insert linebreak character sequences into a string in C#. 

Solution:

The platform safe way of doing this is by using the Environment.NewLine property.


msg = "An error occurred: " + Environment.NewLine + e.Message;

Composing a string with more than one line break can become really messy, so using string.Format might be a good idea.


msg = string.Format("{1} An error occurred!{0}Exception: {0}{2}",
  Environment.NewLine, CurrentTime, e.ToString());

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.