我有一些乍一看對我來說毫無意義的代碼。
問題:這里是否發生了某種隱式參考?似乎只添加了不匹配的專案,而“if”的第一部分什么也沒做。我這樣做對嗎?(我已將我的評論(///)添加到我所理解的內容中以清除我的問題)。(//) 注釋是原始代碼注釋
public static List<BAPresModel> MergeBudgetApprovalByProgramPMLists(
List<BAPresModel> sourceList, List<BAPresModel> destList)
{
/// we create this and use it but it seems to never impact destList that I can tell
BAPresModel dstBA = new BAPresModel();
// Merge sourceList into destList.
foreach (BAPresModel sl in sourceList)
{
var qryDestList = from dl in destList
where sl.FiscalYearID == dl.FiscalYearID
where sl.X == dl.X
where sl.Y == dl.Y
select dl;
if (qryDestList.Count() > 0) // we found a match in the destination list.
{
/// I researched -First()- but don't see anything clarifying my question
/// Is this creating a reference back to destList from dstBA somehow?
dstBA = qryDestList.First();
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
}
else // no match, so add the sourceList item to the destination list.
{
destList.Add(sl);
}
}
/// as far as I can tell the only thing this method actually does is return the original
/// destList with 'sl' matched items added to it
/// all of the work on dstBA is useless??
return destList;
}
感謝您的時間和評估
uj5u.com熱心網友回復:
假設 BPAPresModel 是一個類
dstBA = qryDestList.First();
有效地獲取指向該查詢回傳的第一個匹配條目的指標。
/// this applies the math (adding sl.X to dstBA.X )
dstBA.X = Maths.AddNullableDecimals(dstBA.X, sl.X);
dstBA.Y = Maths.AddNullableDecimals(dstBA.Y, sl.Y);
所以這 2 行在原來的 destList 中改變了它
c# 中的型別別稱為“參考”型別。因為在諸如
BAPresModel foo = SomeFunction();
foo 是對堆上實際物件的參考。如果您是 ac 或 c 開發人員,請考慮
BAPresModel *foo = SomeFunction();
所以不是“隱式”參考,而是“實際”參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/434886.html
