C#字典值清除当我清除以前分配给它的列表....为什么? [英] C# dictionary value clearing out when I clear list previously assigned to it....why?

查看:987
本文介绍了C#字典值清除当我清除以前分配给它的列表....为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dictionary <string, List <SaleItem>> saleItemNew = new Dictionary<string, List<    SaleItem>> ();

saleItems = new List <SaleItem> ();

saleItemNew.Add("1", saleItems);

此时,字典中的列表具有值。

saleItems.Clear();

现在是空的...为什么?

However, when I clear out the list previously assigned to it, the dictionary's value List is now empty...why?

推荐答案

字典包含与列表相同的引用,因此修改列表将更改两个引用。

The dictionary contains the same reference to the list, so modifying the list will change both references.

有关引用类型的Microsoft文档: http://msdn.microsoft.com/en-us/library/490f96s2.aspx

Microsoft documentation about reference types: http://msdn.microsoft.com/en-us/library/490f96s2.aspx

这篇关于C#字典值清除当我清除以前分配给它的列表....为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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