我有一个字典,将从查询结果中填充.因此,我不知道在初始化它时会进入字典的数据值(虽然显然我知道将使用哪些数据类型).我是C#的新手 - 我怎么设置它?
在伪代码中,我想要的字典结构是:
{
"visa": [2.75, 3.33],
"mastercard": [1.00, 4.32],
...
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止,但它没有编译:
//initialize the dictionary but do not populate yet
Dictionary<string, List<decimal>> cardtype_total_amount;
//simulate getting the first card type from the db
string cardtype = "visa";
//initialize the "visa" key
if (!cardtype_total_amount.ContainsKey(cardtype)) cardtype_total_amount.Add(cardtype, new List<decimal>(){0, 0});
//simulate updating the values for "visa" from the db (this would happen lots of times for each card type):
cardtype_total_amount[cardtype][0] += 0.5;
cardtype_total_amount[cardtype][1] += 1.7;
//add more keys for other cardtypes, and update their totals as per above...
Run Code Online (Sandbox Code Playgroud)
我想你只是错过了一个初始化!
//initialize the dictionary but do not populate yet
Dictionary<string, List<decimal>> cardtype_total_amount = new Dictionary<string, List<decimal>>();
Run Code Online (Sandbox Code Playgroud)
[编辑]哦,你需要一些小数点后面的m,否则它们是双打的:
cardtype_total_amount[cardtype][0] += 0.5m;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |