Alex Rivera | Logout

What does {0} mean when found in a string in C#?

Asked 2009-02-09T23:28:12.997
89

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?

Edit
Report

2 Answers

29

It's a placeholder in the string.

For example,

string b = "world.";

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

would produce this output:

Hello world.

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.

answered 2009-02-09T23:33:49.207
4

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 a numeric variable (examples here).

answered 2009-02-09T23:30:50.713

Your Answer