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

本文关键字:2个 定义 单独 变量 循环 list Dictionary | 更新日期: 2023-09-27 18:18:23

这是我的代码到目前为止,我有麻烦定义字符串和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;   
 }

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

正确的语法是

 // 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属性