मैं निम्नलिखित कोड के साथ एक त्रुटि मिलती रहती है:तर्क अपवाद "एक ही कुंजी के साथ आइटम पहले से ही जोड़ दिया गया है"
Dictionary<string, string> rct3Features = new Dictionary<string, string>();
Dictionary<string, string> rct4Features = new Dictionary<string, string>();
foreach (string line in rct3Lines)
{
string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);
rct3Features.Add(items[0], items[1]);
////To print out the dictionary (to see if it works)
//foreach (KeyValuePair<string, string> item in rct3Features)
//{
// Console.WriteLine(item.Key + " " + item.Value);
//}
}
त्रुटि कह एक ArgumentException
फेंकता है,
"के साथ एक आइटम एक ही कुंजी पहले से ही जोड़ा जा चुका है। "
कई Google खोजों को ठीक करने के बाद मैं अनिश्चित हूं।
कोड मैं एक तुलना समारोह के लिए शब्दकोश का उपयोग करने की जरूरत है बाद में:
Compare4To3(rct4Features, rct3Features);
public static void Compare4To3(Dictionary<string, string> dictionaryOne, Dictionary<string, string> dictionaryTwo)
{
//foreach (string item in dictionaryOne)
//{
//To print out the dictionary (to see if it works)
foreach (KeyValuePair<string, string> item in dictionaryOne)
{
Console.WriteLine(item.Key + " " + item.Value);
}
//if (dictionaryTwo.ContainsKey(dictionaryOne.Keys)
//{
// Console.Write("True");
//}
//else
//{
// Console.Write("False");
//}
//}
}
इस समारोह पूरा नहीं किया है, लेकिन मैं इस अपवाद को हल करने की कोशिश कर रहा हूँ। इस अपवाद त्रुटि को हल करने के तरीके क्या हैं, और इस फ़ंक्शन के उपयोग के लिए शब्दकोश तक पहुंच रखें? धन्यवाद
एक ही कुंजी के साथ कई आइटम जोड़ने की कोशिश करना बंद करो। शायद यह सुनिश्चित करने के लिए कि कुंजी पहले से मौजूद नहीं है, आपको एक चेक जोड़ना चाहिए। अन्यथा हमारे पास यह जानने का कोई तरीका नहीं है कि कार्रवाई का सही तरीका क्या होगा क्योंकि आपने डुप्लिकेट आइटम होने पर क्या करना चाहते हैं, इसके बारे में लगभग कोई जानकारी नहीं दी है। –
आपके पास डुप्लिकेट कुंजी क्यों हैं? यदि आपको प्रति कुंजी एक से अधिक मान की आवश्यकता है, तो आपको 'शब्दकोश <स्ट्रिंग, सूची> 'का उपयोग करना चाहिए। –
अधिक जानकारी के लिए नीचे मेरा उत्तर देखें, लेकिन आप अपने एड को एक अलग वाक्यविन्यास से बदल सकते हैं। यदि कुंजी पहले से मौजूद है तो निम्न कुंजी के लिए मौजूदा मान को ओवरराइट कर देगा: 'rct3 विशेषताएं [आइटम [0]] = आइटम [1];' –