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

2019-01-03 23:57发布

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?

7条回答
混吃等死
2楼-- · 2019-01-04 01:02

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 four spaces.

There are a number of built-in format specifiers, and in addition, you can make your own. For a decent tutorial/list see String Formatting in C#. Also, there is a FAQ here.

查看更多
登录 后发表回答