在Dictionary列表中为每个循环定义2个单独的变量

时间:2016-11-17 19:09:11

标签: c# arraylist

到目前为止,这是我的代码,我在定义从每个循环内的列表数组中选择的字符串和UInt32时遇到了问题。谁能告诉我如何解决这个问题呢?

 Dictionary<string, UInt32> cars = new Dictionary<string, UInt32>();

 cars.Add("Vehicle1", 4294967295);
 cars.Add("Vehicle2", 6329496762);

 foreach (KeyValuePair<string, UInt32> pair in cars)
 {
     string vehiclename = pair.Item1; 
     UInt32 vehicledata = pair.Item2;   
 }

1 个答案:

答案 0 :(得分:1)

正确的语法是

 // You have to switch to long from UInt32 since... 
 Dictionary<string, long> cars = new Dictionary<string, long>();

 cars.Add("Vehicle1", 4294967295L);
 cars.Add("Vehicle2", 6329496762L); // ... this value is greater than UInt32.MaxValue

 // var: you have no need to put such a long declaration as KeyValuePair<string, long>
 foreach (var pair in cars)
 {
     string vehiclename = pair.Key; 
     long vehicledata = pair.Value;   
 }

请注意,与KeyValuePair<string, long>不同的Tuple<string, long>包含KeyValue属性