I basically have a list of Dictionaries like:
List<Dictionary<string, string>>
For the purposes of testing I'm fetching 36 dictionary items into the list and then returning the list at the end of my function.
The odd thing is when I populate the list, I can see the Key=>Value pairs of the dictionaries being added to the list in the Visual Studio Inspector, however upon clearing the original dictionary used to populate my list, all that remains is 36 empty items in the list.
Is there some weird List behaviour happening that I'm unaware of? A code snip is included below for reference...
List<Dictionary<string, string>> allResults = new List<Dictionary<string, string>>();
Dictionary<string, string> selectResult = new Dictionary<string, string>();
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataReader = cmd.ExecuteReader();
try
{
while (dataReader.Read())
{
for (int i = 0; i < dataReader.FieldCount; i++)
{
selectResult.Add(dataReader.GetName(i).ToString(), dataReader.GetValue(i).ToString());
}
allResults.Add(selectResult);
//Something to do with this next line seems to cause the List to also lose the values stored in the Dictionary, is clearing the dictionary not allowed at this point and the list is simply referencing the Dictionary rather than 'making a copy'?
selectResult.Clear();
}
dataReader.Close();
}
catch { }
this.Close();
return allResults;