C#将元素添加到字典中,键作为字符串,值作为另一个字典 [英] C# Add elements to Dictionary with key as string and value as another Dictionary

查看:112
本文介绍了C#将元素添加到字典中,键作为字符串,值作为另一个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我有一个字典diskInfo,它的键是字符串,值是字符串类型double的字典.对于foreach循环中的每个条目,我都会计算磁盘空间并将结果存储到spaceInfo字典中.然后将其传递为存储为diskInfo字典中相应键的值.

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.

问题每次我清除spaceInfo中的现有条目并读取键值对,然后再存储到diskInfo字典中.在循环结束时,diskInfo词典已正确存储了键,但是所有键的值计数为0.我是使用字典的新手,所以请您帮忙解释为什么会发生这种情况.

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.

谢谢

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();
        }

推荐答案

尝试类似的操作

diskInfo.Add(Convert.ToString(mo["Name"]), new Dictionary<string, double>(spaceInfo));

由于浅复制而导致的问题.您必须执行深层复制,以避免此类问题.Dictionary 类中的复制构造函数可能会帮助您解决问题,因为它会从 spaceInfo 对象创建新实例.

The problem because of shallow copying. You must perform deep copying to avoid such problems. The copy constructor in the Dictionary class might help you solving the problem since it creates new instance from the spaceInfo object.

但是更好的方法是为SpaceInformation创建类.

But the better way to do this is by creating class for the SpaceInformation.

class SpaceInfo
{
    private double _size;
    private double _free;
    private double _percentFree;

    public double Size
    {
        get;
        set;
    }

    public double Free 
    {
        get;
        set;
    }

    public double PercentFree
    {
        get;
        set ;
    }

    public SpaceInfo(double size, double free)  
    {
        Size = size;
        Free = free;
        PercentFree = (free / size) * 100;
    }
}

 /*Inside the loop*/
 double size = Convert.ToDouble(mo["Size"]) / divisor;
 double free = Convert.ToDouble(mo["FreeSpace"]) / divisor;
 SpaceInfo spaceInfo = new SpaceInfo(size, free);
 Dictionary<string, SpaceInfo> diskInfo = new Dictionary<string, SpaceInfo>();
 diskInfo.Add(Convert.ToString(mo["Name"]), spaceInfo);



`

这篇关于C#将元素添加到字典中,键作为字符串,值作为另一个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆