Adding two consecutive new lines in a C# statement


I need to add two consecutive new lines in a statement as in the code below.

I used Enviroment.NewLine + Enviroment.NewLine and got an error.
Can you show me how to go about doing it. Thanks?

string Note_Text = " On " + NoteDate + " " + "at " + NoteTime + " " + NoteOwner + "  " + "wrote :" + Environment.NewLine + Environment.NewLine + getTheNote.NoteText;

Open in new window

Who is Participating?
as dale said, what's the error you're receiving?
anyway... I remember one time I had to use a constant for that

public static string sReturn = System.Environment.NewLine;

and then in my strings use the sReturn, because I was getting an error but can't recall if it was exactly because of the two consecutive newlines... I find that odd...
anyway, I have a project in where I have a string like

strMessage = "blah blah blah " + sReturn + sReturn + "more blah blah blah";

and it works perfectly
Dale BurrellDirectorCommented:
What was the error?
adamtraskAuthor Commented:
The name Environment doesn't exist in the current content
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

adamtraskAuthor Commented:
Thank you baretree:

It worked just as string variable:

string sReturn = System.Environment.NewLine;
Dale BurrellDirectorCommented:
Try fully qualifying it (or make sure the system namespace is included) e.g. System.Environment.NewLine
käµfm³d 👽Commented:
You might find string.Format a tad easier to work with (and read!!) than normal string concatnation. Using string.Format, you line above could be written as:

string Note_Text = string.Format(" On {0} at  {1} {2} wrote : {3}{3}{4}", NoteDate, NoteTime, NoteOwner, Environment.NewLine, getTheNote.NoteText);

Open in new window

Notice there are placeholders in the above string. The placeholders are of the form "{N}", where "N" is a number, starting at zero. Just like array indexes, format placeholders start at zero, and each placeholder that represents a different object gets a new number--incremented by one from the last number used. For the above, "{0}" corresponds to "NoteDate", since "NoteDate" is the first parameter to string.Format after the initial string, "{1}" corresponds to "NoteTime", "{2}" corresponds to "NoteOwner", both "{3}" correspond to "Environment.NewLine", and "{4}" corresponds to "getTheNote.NoteText". Using something like the above makes it a tad easier to debug improperly output strings you are building in your code.
Dale BurrellDirectorCommented:
It worked because you fully qualified it e.g. System.Environment.NewLine - you won't need to make it a local variable.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.