Background: I have a dictionary diskInfo with key as string and value as dictionary of type string, double. For each entry into the foreach loop, I calculate the disk space and store the result into spaceInfo dictionary. This is then passed on to be stored as the value for corresponding key in diskInfo dictionary.
Problem Each time I clear existing entries in spaceInfo and readd the key value pairs before storing into diskInfo dictionary. At the end of the loop, diskInfo dictionary has keys stored in correctly, but count of values is 0 for all keys. I am new to using Dictionaries, so could you please help in explaining why this happens.
Thankyou
Dictionary<string, double> spaceInfo = new Dictionary<string, double>();
Dictionary<string, Dictionary<string, double>> diskInfo = new Dictionary<string, Dictionary<string, double>>();
foreach (ManagementObject mo in queryCollection)
{
double size = Convert.ToDouble(mo["Size"]) / divisor;
double free = Convert.ToDouble(mo["FreeSpace"]) / divisor;
double percentFree = (free / size) * 100;
spaceInfo.Add("size",size);
spaceInfo.Add("freeSpace",free);
spaceInfo.Add("percentFree",percentFree);
diskInfo.Add(Convert.ToString(mo["Name"]),spaceInfo);
spaceInfo.Clear();
}