C# 在将对象作为值插入集合后重用该对象
在使用哈希表时,我遇到了以下问题,但这是一个普遍问题: 假设我有:C# 在将对象作为值插入集合后重用该对象,c#,reference,C#,Reference,在使用哈希表时,我遇到了以下问题,但这是一个普遍问题: 假设我有: 某些类(例如,Cat),包含一些成员(例如,publicAge属性) 带有一些键(例如,字符串)和列表值(例如,列表)的哈希表 是否有一种方法可以实例化列表的单个实例并重用它,从而以理想的方式产生结果: Hashtable catDictionary = new Hashtable(); Cat cat1 = new Cat() { Age = 10 }; Cat cat2 = new Cat() { Age = 12
Cat
),包含一些成员(例如,publicAge
属性)字符串
)和列表
值(例如,列表
)的哈希表李>
是否有一种方法可以实例化列表的单个实例
并重用它,从而以理想的方式产生结果:
Hashtable catDictionary = new Hashtable();
Cat cat1 = new Cat() { Age = 10 };
Cat cat2 = new Cat() { Age = 12 };
List<Cat> catList = new List<Cat>();
catList.Add(cat1);
catList.Add(cat2);
catDictionary["oldCat"] = catList;
catList.Clear(); // This undesirably clears the list in HashTable["oldCat"]
Cat cat3 = new Cat() { Age = 2 };
catList.Add(cat3); // Now the list in HashTable["oldCat"] references a young cat!
catDictionary["youngCat"] = catList;
Hashtable catDictionary=newhashtable();
Cat cat1=新的Cat(){Age=10};
Cat cat2=新Cat(){Age=12};
List catList=新列表();
catList.Add(cat1);
catList.Add(cat2);
catDictionary[“oldCat”]=catList;
catList.Clear();//这不希望清除哈希表[“oldCat”]中的列表
Cat cat3=新Cat(){Age=2};
catList.Add(cat3);//现在HashTable[“oldCat”]中的列表引用了一只小猫!
catDictionary[“youngCat”]=catList;
我的意思是,对HashTable[“oldCat”]
中List
的引用能否获得插入的列表的自己的副本,以便将其与main
中引用的副本“分离”
关于重用Cat
对象,我想我也可以问同样的问题,但是重用列表在某种程度上感觉更有用。您将插入词典的列表必须是一个新列表,否则您对主列表所做的任何更改都将影响词典列表(因为它们基本上是同一个实例,有两个引用)。
我还建议您使用通用词典而不是哈希表
尝试使用以下方法创建列表的新副本:
Dictionary<string, List<Cat>> catDictionary = new Dictionary<string, List<Cat>>();
Cat cat1 = new Cat() { Age = 10 };
Cat cat2 = new Cat() { Age = 12 };
List<Cat> catList = new List<Cat>();
catList.Add(cat1);
catList.Add(cat2);
catDictionary["oldCat"] = catList.ToList();
catList.Clear();
Cat cat3 = new Cat() { Age = 2 };
catList.Add(cat3);
catDictionary["youngCat"] = catList.ToList();
Dictionary catDictionary=newdictionary();
Cat cat1=新的Cat(){Age=10};
Cat cat2=新Cat(){Age=12};
List catList=新列表();
catList.Add(cat1);
catList.Add(cat2);
catDictionary[“oldCat”]=catList.ToList();
catList.Clear();
Cat cat3=新Cat(){Age=2};
catList.Add(cat3);
catDictionary[“youngCat”]=catList.ToList();
您必须创建一个新列表,否则它将是相同的列表
Hashtable catDictionary = new Hashtable();
Cat cat1 = new Cat() { Age = 10 };
Cat cat2 = new Cat() { Age = 12 };
List<Cat> catList = new List<Cat>();
catList.Add(cat1);
catList.Add(cat2);
catDictionary["oldCat"] = new List<Cat>(catList);
catList.Clear(); // This undesirably clears the list in HashTable["oldCat"]
Cat cat3 = new Cat() { Age = 2 };
catList.Add(cat3); // Now the list in HashTable["oldCat"] references a young cat!
catDictionary["youngCat"] = catList;
Hashtable catDictionary=newhashtable();
Cat cat1=新的Cat(){Age=10};
Cat cat2=新Cat(){Age=12};
List catList=新列表();
catList.Add(cat1);
catList.Add(cat2);
catDictionary[“oldCat”]=新列表(catList);
catList.Clear();//这不希望清除哈希表[“oldCat”]中的列表
Cat cat3=新Cat(){Age=2};
catList.Add(cat3);//现在哈希表[“oldCat”]中的列表引用了一只小猫!
catDictionary[“youngCat”]=catList;
将cat3添加到新列表中,否则它将被引用到相同的列表。您必须使用First()、Last()或Take()来获取单个项目。或者使用Select/Where在列表中枚举。