我有两个属性是列表的类,其中一个内容是 int - 这是来自第二个列表的对象的 ID。我覆盖 setter 和 getter 以使它们彼此一致。但是当我添加一些这个到列表时,它们是不同步的。如何使它们同步?
这是代码
public class Item
{
private List<Operation> _operations = new List<Operation>();
private List<int> _operationsID = new List<int>();
public List<Operation> operations
{
get { return this._operations; }
set
{
this._operations = value;
if (value != null)
{
foreach (Operation oper in value)
{
this._operationsID.Add(oper.ID);
}
}
}
}
public List<int> operationsID
{
get { return this._operationsID; }
set
{
this._operationsID = value;
if (value != null)
{
foreach (int operID in value)
{
this._operations.Add(new Operation(operID));
}
}
}
}
}
如果是这样,我应该覆盖 List.Add,我该怎么做?