是什么{0}意味着在C#中的字符串时发现? [英] What does {0} mean when found in a string in c#?
问题描述
在这样的字典:
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"]);
输出是:
有关键为RTF格式值= wordpad.exe
For Key = "rtf" value = wordpad.exe
这是什么{0}意味着什么?
What does the {0} means?
感谢您。
推荐答案
您打印的格式化字符串。 {0}表示插入格式字符串后的第一个参数;在这种情况下,用钥匙RTF格式。
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".
有关的String.Format,这是类似的,如果你有这样的事情
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 )
您需要创建一个字符串是一个测试,其值为 42
you'd create a string "This is a test. The value is 42".
您也可以使用前pressions,并多次打印出来的值:
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)
得到纤维蛋白原: 1 1 2 3
更多信息请访问<一个href=\"http://msdn.microsoft.com/en-us/library/txafckwd.aspx\">http://msdn.microsoft.com/en-us/library/txafckwd.aspx,其中谈到了复合材料的格式。
See more at http://msdn.microsoft.com/en-us/library/txafckwd.aspx, which talks about composite formatting.
这篇关于是什么{0}意味着在C#中的字符串时发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!