Why can't I write MessageBox.Show ("asdfasdf {0}", i) ;?

int i = 85; 
Console.WriteLine("My intelligence quotient is {0}", i);  // Kosher
MessageBox.Show("My intelligence quotient is {0}", i); // Not Kosher

I find this the most terrible debilitating. Does one work and not the other? What is the source of this behavioral mismatch? The more I think about it, the less I can imagine, and the inability to understand too often turns into arrogance.

+5
source share
6 answers

Console.Writeline has the following overloads:

In particular, an overload that takes a format string and params array .

Here is another method that is very similar:

, MessageBox.Show . , , .

, string.Format:

public void ShowMessageBox(string format, params object[] args)
{
    MessageBox.Show(string.Format(format, args));
}

// ...

ShowMessageBox("You entered: {0}", someValue);
+3

Show() .

Console.WriteLine , .NET.

, string.Format :

MessageBox.Show(string.Format("asdfasdf{0}", i)); // Kosher
+9

( MS), "" , string.Format - , :

MessageBox.Show (string.Format ("asdfasdf{0}", i));

Console.WriteLine (string.Format ("asdfasdf{0}", i)); // although this is unneccesary!
+1

WriteLine() WriteLine ( , Object arg0) MessageBox.Show() . :

MessageBox.Show(string.Format("asdfasdf{0}", i));
0

Console.WriteLine, Debug.Print .. - , , . MessageBox.Show - , . (, , ..), .

0

, , :

 class myMessageBox
    {
        private myMessageBox()
        { }

        public static void Show(string text,params object[] i)
        {
            text = String.Format(text, i);
            MessageBox.Show(text);
        }
    }
0

All Articles