Frage

I've recently started learning C#.
I have an string type of variable a.

I'm trying to get a Messagebox to show my variable and some text after it.

MessageBox.Show(a "was your answer"); This doesn't work.
MessageBox.Show(a, "was your answer"); While this throws the text to the title.

How could I make some text appear after the variable, on the same line?

War es hilfreich?

Lösung

Try

MessageBox.Show(a + "was your answer");

or

MessageBox.Show(string.Format("{0} was your answer", a));

Using

string.Format()

can be neater for multiple string variables and easier to change the string literal if you need to. See this SO question for a discussion on its use.

Your

MessageBox.Show(a, "was your answer");

throws the text to the title because the method signature of MessageBox.Show() that takes two arguments is for:

public static DialogResult Show(
    string text,
    string caption
)

Displays a message box with specified text and caption.

MSDN

Andere Tipps

You need concatenation

MessageBox.Show(a + "was your answer");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top