I having a problem with output parameter in C#
I have the following function:
public Financial Calculate(int dealerPricingID, int customerID, bool dealerPriceChanged, bool saving, int specialBidID, int promotionID, List<Item> outItems)
The parameter outItems is an output parameter and before calling the function I just initialize it with
var items = new List<Item>();
I thought that since a list is a reference type it should end up modified after the function is executed (in the function the list is filled with Item objects that are affected in the calculation). However, it stays unmodified and after debugging I see that inside the function items are add to the collection it just returns empty.
It seems I have some of my concepts of C# wrong, so why is this not working in this situation?
You haven’t re-initialized
outItemsinsideCalculate, have you? If you just modify the elements, it should work fine. If you assign anotherList<Item>tooutItems, you modify the reference which is copied.If you want to pass a reference to the reference, use the
refkeyword.