Ari*_*MAZ 0 c# dictionary winforms
打印字典的最佳方法是什么,我创建了Currency类,如下所示
class Currency{
public string currency_code { get; set; }
public float unit { get; set; }
public string currency_name { get; set; }
public string currency_isim { get; set; }
public float forex_buying { get; set; }
public float forex_selling { get; set; }
public float banknote_buying { get; set; }
public float banknote_selling { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有没有办法自动打印对象?
我想尽可能打印出来
Dictionary<string, Currency> dict = new Dictionary<string, Currency>();
.....
.....
add data to dict properly
.....
.....
dict.print();
Run Code Online (Sandbox Code Playgroud)
我希望它返回一个包含该对象的所有数据的正确字符串.我怎样才能做到这一点?
public class Currency
{
...
public override string ToString()
{
return String.Format("Currency Code: {0}\nUnit:{1}...",...);
}
}
foreach (string key in dict.Keys)
{
Console.WriteLine("Key: {0}\nValue{1}", key, dict[key].ToString());
}
Run Code Online (Sandbox Code Playgroud)
覆盖ToString,遍历所有键/值,然后打印.