views:

826

answers:

8

Hi,

In a dictionary like this:

Dictionary<string, string> openWith = new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

The output is:

For Key = "rtf" value = wordpad.exe

What does the {0} means?

Thank you.

+29  A: 

You are printing a formatted string. The {0} means to insert the first parameter following the format string; in this case the value associated with the key "rtf".

For String.Format, which is similar, if you had something like

//            Format string                    {0}           {1}
String.Format("This {0}.  The value is {1}.",  "is a test",  42 )

you'd create a string "This is a test. The value is 42".

You can also use expressions, and print values out multiple times:

//            Format string              {0} {1}  {2}
String.Format("Fib: {0}, {0}, {1}, {2}", 1,  1+1, 1+2)

yielding "Fib: 1, 1, 2, 3"

See more at http://msdn.microsoft.com/en-us/library/txafckwd.aspx, which talks about composite formatting.

Daniel LeCheminant
+2  A: 

It's a placeholder for the first parameter, which in your case evaluates to "wordpad.exe".

If you had an additional parameter you'd use {1} etc.

Bravax
+2  A: 

It's a placeholder for a parameter much like the %s format specifier acts within printf.

You can start adding extra things in there to determine the format too, though that makes more sense with numeric variable (examples here).

Mat
+2  A: 

Please read the documentation rather than asking us to explain stuff to you. The answer, with lots of examples, is on MSDN. It is more than acceptable to ask questions once you've gone as far as you can on your own, but I question if you looked for an answer at all.

abelenky
Why'd I get down-voted? I answer the question, and teach the OP to fish.
abelenky
Don't need to be so rude, We know you are smart
Rulas
+1 - much better than the original, which was less about teaching the OP to fish than it was about berating him for not fishing in the first place. =P
Erik Forbes
The purpose of this board really is to give answers, though, so the tone of this answer is still inappropriate. If you don't want to ask questions or give answers, why are you here?
Kyralessa
If people read documentation, SO wouldn't exist. Or rather, SO would be all fluff questions prefaced with "As a programmer...."
Robert S.
Kyralessa / OutIntoSpace: most of the questions on SO are NOT immediately answered by documentation. Questions about complexity, algorithms, patterns, style, and approaches are never something you can open a manual and immediately learn.
abelenky
The MSDN documentation on this subject is dense, wordy, and overkill for what the questioner wants to know. Daniel L's answer is a good example of something concise and more helpful. At most, you should provide an MSDN link "for further detail" *after* giving a good concise answer.
Kyralessa
+2  A: 

It's a place holder in the string. For example:

string b = "world.";

Console.WriteLine("Hello {0}", b);

Would produce this output:

Hello world.

Edit: Also, you can have as many placeholders as you wish. This also works on String.Format.

string b = "world.";
string a = String.Format("Hello {0}", b);

Console.WriteLine(a);

And you would still get the very same output.

M4dRefluX
+2  A: 

In addition to the value you wish to print, the {0} {1} etc. you can specify a format. For example, {0,4} will be a value that is padded to 4 spaces. There are a number of built-in format specifiers, and in addition, you can make your own. For a decent tutiorial/list see http://blog.stevex.net/index.php/string-formatting-in-csharp/ also, there is a FAQ here

Muad'Dib
A: 

Thanks all.

I got it now.

Ricardo
Perhaps you could flag the correct answer as accepted?
Zaagmans
Then accept daniel's answer =)
John T
This is not an "answer". The appropriate response is to accept the reply you like best (with the green-check mark on the left)
abelenky
I think it even rewards you when you select an "accepted" answer to your question (to encourage you to do so, because it also rewards the person who provided the best answer).
Rob Parker
+2  A: 

For future reference, in Visual Studio you can try placing the cursor in the method name (eg. WriteLine) and press F1 to pull up help on that context. Digging around should then find you String.Format() in this case, with lots of helpful info.

Note that highlighting a selection (eg. double-clicking or doing a drag-select) and hitting F1 only does a non-context string search (which tends to suck at finding anything helpful), so make sure you just position the cursor anywhere inside the word without highlighting it.

This is also helpful for documentation on classes and other types.

Rob Parker