简体   繁体   English

c#ArgumentOutOfRangeException

[英]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 the List has items, but it does not enter inside the 'if' statement. 问题是有时List有项目,但它不会进入'if'语句。

Check the images below of a debug session: 检查调试会话的以下图像:

在此输入图像描述

How is that even possible? 这怎么可能呢?

But if I force it to return the first item, on the last return statement, I get an ArgumentOutOfRangeException even though the list has elements. 但是如果我强制它返回第一个项目,在最后一个return语句中,即使列表中有元素,我也会得到一个ArgumentOutOfRangeException。 Check the nest image: 检查嵌套图像:

在此输入图像描述

Is there any problem with my code or it is just a stupid bug? 我的代码有什么问题,或者它只是一个愚蠢的错误?

Update: 更新:

The LayPrices List is only instantiated on the class Constructor: LayPrices = new List<PriceStruct>(); LayPrices List仅在类Constructor上实例化: LayPrices = new List<PriceStruct>(); .

It is only getting filled with items on one method, whith 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 issues and threading were my first clue, so I did put a lock(LayPrices) and the problem still persisted: 并发问题和线程是我的第一个线索,所以我确实放了一个锁(LayPrices),问题仍然存在:

锁

So I think it is not a concurrency issue. 所以我认为这不是一个并发问题。

Put Debug.Assert(LayPrices.Count > 0) in the getter before the if statement and you'll see that the List is in fact empty. Debug.Assert(LayPrices.Count > 0)放在if语句之前的getter中,你会发现List实际上是空的。

The only reasonable explanation is that you are populating the list either in some other thread and you have a race condition or in a property getter that only gets fired by the debugger (you could also be populating the list in a catch clause up the callstack but I imagine you would have figured that out by yourself) 唯一合理的解释是你在一些其他线程中填充列表并且你有竞争条件或者只在调试器中触发的属性getter(你也可以在callstack中的catch子句中填充列表但是我想你会自己想出来的)

In order to get a better answer please include all code that populates your list. 为了获得更好的答案,请包含填充列表的所有代码。 No only the code you think should run, but all the properties, constructors or methods that add or remove items from the list. 不仅您认为应该运行的代码,还有从列表中添加或删除项目的所有属性,构造函数或方法。

I found the problem. 我发现了这个问题。 It was indeed concurrency issues, even though I don't use threads explicitly, I use events and I thought event handling was synchronized (It is synchronized right?). 这确实是并发问题,即使我没有明确地使用线程,我使用事件并且我认为事件处理是同步的(它是同步的吗?)。

If I add locks everywhere I read or add to the list, the problem disappears. 如果我在读到或添加到列表的任何地方添加锁,问题就会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM