我有一个字典myDataItems,其中包含string和double。我将值从2个文本框传递到字典,1个文本框传递字符串值,另一个传递双精度值。我想知道如何在第3个文本框中显示字典项目(txtOutput)我用于输出目的?
public partial class Form1 : Form
{
private Dictionary<string, double> myDataItems = new Dictionary<string, double>();
private string dataName;
private double dataCost;
private void dataItemSend_Click(object sender, EventArgs e)
{
dataName = dataNameInput.Text;
dataCost = Convert.ToDouble(dataCostInput.Text);
myDataItems.Add(dataName, dataCost);
txtOutput.Text = myDataItems.ToString();
}
}
答案 0 :(得分:1)
一种简单的方法:
var lines = myDataItems.Select(kv => kv.Key + ": " + kv.Value.ToString());
txtOutput.Text = string.Join(Environment.NewLine, lines);
请务必添加using System.Linq;
答案 1 :(得分:0)
txtOutput.Lines = myDataItems.Select(x => x.Key + " : " + x.Value).ToArray();