C # ArgumentOutOfRangeException

I have this piece of code:

    private List<...> LayPrices;
    public Decimal BestLayOdds
    {
        get
        {
            if (LayPrices.Count > 0)
            {
                return LayPrices[0].dOdds;
            }
            return 1000.0M;   
        }
    }

The problem is that sometimes there are elements in the List, but it is not included in the 'if' statement.

Check the images below the debugging session:

enter image description here

How is this possible?

But if I force it to return the first element, in the last return expression I get an ArgumentOutOfRangeException exception, even if there are elements in the list. Check the slot image:

enter image description here

Is there a problem with my code or is it just a stupid mistake?

Update:

The list LayPricesis created only in the Constructor: class LayPrices = new List<PriceStruct>();.

It is filled only with elements according to one method using the following code:

    LayPrices.Clear();
    foreach (PriceSize priceSize in exchangePrices.availableToLay)
    {
          PriceStruct lay = new PriceStruct();
          lay.dOdds = (Decimal)priceSize.price;
          lay.dAmount = (Decimal)priceSize.size;

          LayPrices.Add(lay);
   }

Concurrency problems and threads were my first clue, so I placed a lock (LayPrices) and the problem still persisted:

lock

, concurrency.

+4
2

. concurrency, , , , ( ?).

, , .

+1

Debug.Assert(LayPrices.Count > 0) getter if, , List .

, - , , ( catch callstack, , )

, , , . , , , , , , .

+1

All Articles