2

考虑这些代码行:

  //prodProdGroup is a list within the itm object that I need to search. The items
  //within the list are of type ProductionCostCalcHelper. I need to find one
  //of the ProductionCostCalcHelper records in the list, calculate its total dollar value
  //and assign it the value

  ProductionCostCalcHelper prodGroupItm = itm.prodProdGroup.SingleOrDefault(f => f.MAST_PROJ.Trim() == laborItm.MAST_PROJ.Trim());
  ProductionCostCalcHelper prodGroupItm2 = itm.prodProdGroup.SingleOrDefault(f => f.MAST_PROJ.Trim() == laborItm.MAST_PROJ.Trim());

  if (prodGroupItm != null)
  {
        prodGroupItm.TOTAL_DOLLAR = avgDollarsPerHour * prodGroupItm.HOURS;
  }

我假设 SingleOrDefault 方法将通过引用返回对象,但事实并非如此。在更改了 ProdGroupItm 的 TOTAL_DOLLAR 金额后,ProdGroupItm2 保持不变,证明它们没有引用列表中的内容。为什么是这样?有没有办法更新列表中对象的值?

4

1 回答 1

2

如果您的ProductionCostCalcHelper类型是 mutable ,就会发生这种情况struct
不要那样做可变结构是邪恶的

每次您传递 astruct时,整个值都会复制到您传递给它的任何位置。

改用一个类。

于 2011-08-03T19:19:25.910 回答