Zan*_*nix 1 c# dictionary messagebox
我正在使用Visual Studio C#,我有一个带有一些记录的"string-string"Dictionary变量,例如:
{Apartment1},{Free}
{Apartment2},{Taken}
Run Code Online (Sandbox Code Playgroud)
等等...
如何在消息框中写这个,以便显示如下内容:
Apartment1 - Free
Apartment2 - Taken
Run Code Online (Sandbox Code Playgroud)
等等...
重要的是每条记录都在消息框中的新行内.
您可以遍历字典中的每个项目并构建一个字符串,如下所示:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
StringBuilder sb = new StringBuilder();
foreach (var item in dictionary)
{
sb.AppendFormat("{0} - {1}{2}", item.Key, item.Value, Environment.NewLine);
}
string result = sb.ToString().TrimEnd();//when converting to string we also want to trim the redundant new line at the very end
MessageBox.Show(result);
Run Code Online (Sandbox Code Playgroud)