C#: How to show a newline within a MessageBox

Viewed 856

I often display messages using MessageBox.Show("This is my message");.
Sometimes I need a newline within a longer text. Usually I use a variant which put one string per line:
MessageBox.Show("Line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3");
But I don't like the "overhead". So I found the following solution:
MessageBox.Show(string.Format("Line1{0}Line2{0}Line3", Environment.NewLine));
Is there a better solution with less overhead?

5 Answers

You have pretty much found both ways to do it. A third one would be with string interpolation (to avoid the "extra" string.Format call.

$"Line1 {Environment.NewLine} Line2 {Environment.NewLine} Line3"

Fiddler example: https://dotnetfiddle.net/16Wy57

The string.Format way is pretty much the example at the official format docs

I prefer interpolation as it avoids the extra function in my code, but really it all comes down to preference.

There really is no "overhead" in any of the options, as the operation is trivial and at the end of the day, you concatenate strings.

If you want a growable solution with less overhead, here's a solution that is clean and scalable:

using System;

var messageLines = new string[]
{
    "Some line here",
    "More lines",
    "Could be loaded from a database",
    "With each row as a string"
};

MessageBox.Show(string.Join(Environment.NewLine, messageLines));

Verbatim strings, just make sure you don't have identations on the string.

string v = 
@"Hello, 
! Today is 
, it's 
now.";

Console.WriteLine(v);

Output:

Hello, 
! Today is 
, it's 
now.

Demo: https://dotnetfiddle.net/WvjGfC

MessageBox.Show("First Line\nSecond Line\nThird Line");
\n inserts a new line in a string which works for MessageBox.
Be careful with this within files. There you find \r\n, \n\r, \r, \n - it depends on the operating system or file format.

Luke Parker's example can be slicked up a bit if you make your own MessageBox class (sadly it can't be done as an extension method because there isn't an available instance of MessageBox):

namespace System.Windows.Forms
{
    public static class MessageBoxLines
    {
        public static void Show(string[] lines) 
        {
            MessageBox.Show(string.Join(Environment.NewLine, lines));
        }
    }
}

You'd use it like:

MessageBoxLines.Show(new[] { "Line1", "Line2"} );

You'd add more overloads or optional parameters for the other forms (those that take a caption, specify the buttons etc


This could be tidied further if you use a params array:

    public static void Show(params string[] lines) 
    {
        MessageBox.Show(string.Join(Environment.NewLine, lines));
    }

Which you'd use like:

MessageBoxLines.Show("Line1", "Line2" );

But using params would possibly interfere with the standard pattern for the other overloads (buttons etc) so you'd have to get more cute working that one out, either by rearranging the order so params is last or by putting all the args into a params object and digging them out / casting them back to what they should be (which clowns intellisense a bit)

Related