After studying your code snippet for a bit here's what I came up with.
I wouldn't call them iterators. They're really acting more as indexing operators that doubles as a loop counter. Furthermore, they're always incremented in sync and they have the exact same initial value.
int oi = 0; // old list iterator
int ni = 0; // new list iterator
So I would replace with simply:
int i = 0;
The second loop for trimming the extra elements:
while (oldList.Count > ni)
The post-condition for
i after the first loop has to be equal to
newList.Count otherwise we would still be looping the first while. Thusly, changing the condition to this is clearer:
while (oldList.Count > newList.Count)
After tracing your
UpdateMarketOrderList method, the discernible end effect, from what I can tell, just seems to be a replacement of the oldList with the newList. If that's really the desired end consequence of running this function, why not simplify it, succinctly to this instead?
public static void UpdateMarketOrderList( ObservableCollection<MarketOrder> oldList,
List<MarketOrder> newList )
for(int i = 0; i < newList.Count; ++i)
Then again you didn't specify in your question what the pre/post-conditions are for
UpdateMarketOrderList, which is something that does matter.
After reading OP's comment that fired change events are important, I would refactor the main loop like this:
while (i < newList.Count)
if (i == oldList.Count ||
oldList[i].Price > newList[i].Price)
else if (oldList[i].Price < newList[i].Price)
else if (oldList[i].Quantity != newList[i].Quantity)
// && oldList[i].Price == newList[i].Price
Debug.Assert(oldList[i].Price == newList[i].Price);
oldList[i].Quantity = newList[i].Quantity;
With this refactor you decrease the indent level and tighten the loop a bit while preserving existing behavior. The key thing to realize is that
RemoveAt doesn't increment the loop counter. Your previous code did that in a slightly less direct manner.
My gut feeling is that this could be refactored further still but without a good idea of the expected side-effects and other requirements for this method, it will be difficult to refactor this safely.
Priceand there are no duplicates per Price? \$\endgroup\$