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}
mean?
For future reference, in Visual Studio you can try placing the cursor in the method name (for example, 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 information.Note that highlighting a selection (for example, 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.
This is what we called Composite Formatting of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. The resulting string is written to the output stream.
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
you'd create a string "This is a test. The value is 42".
You can also use expressions, and print values out multiple times:
yielding "Fib: 1, 1, 2, 3"
See more at http://msdn.microsoft.com/en-us/library/txafckwd.aspx, which talks about composite formatting.
It's a placeholder for a parameter much like the
%s
format specifier acts withinprintf
.You can start adding extra things in there to determine the format too, though that makes more sense with a numeric variable (examples here).
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.It's a placeholder in the string.
For example,
would produce this output:
Also, you can have as many placeholders as you wish. This also works on
String.Format
:And you would still get the very same output.