2016-11-17 23 views
0

這是我的代碼到目前爲止,我無法定義從每個循環內的列表數組中選擇的字符串和UInt32。有誰能告訴我如何解決這個問題嗎?在詞典列表中爲每個循環定義2個單獨的變量

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

正確的語法是

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

+2

另外,'6329496762'比'uint.MaxValue'更大。 – Quantic

+0

@Quantic:好的,趕上!謝謝! –

+0

@DmitryBychenko謝謝 – Offset

相關問題