jotbek
jotbek

Reputation: 1499

Debugging List<T>.Count throws ArgumentException

I have external class Item that if I make:

List<Item> items = new List<Item>();

that hovering mouse on it during debugging shows

'items.Count' threw an exception of type 'System.ArgumentException' int {System.ArgumentException}

I cannot share the whole code of this class but what might be the reason. I decompiled class and I see that there are GetHashCode and Equals method overriden. May it be a cause?

Edit:

In debug, after line

List<Item> items = new List<Item>();

using Immediate window and writing there items.Count I get:

'items.Count' threw an exception of type 'System.ArgumentException'
base {System.SystemException}: {"Cannot find the method on the object instance."}
Message: "Cannot find the method on the object instance."
ParamName: null

Upvotes: 2

Views: 1740

Answers (1)

user7116
user7116

Reputation: 64098

A quick look at ILSpy or Reflector shows that List<T>.Count could not possibly raise that exception.

/// <summary>Gets the number of elements actually contained in the
///   <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <returns>The number of elements actually contained in the
///   <see cref="T:System.Collections.Generic.List`1" />.</returns>
public int Count
{
    get { return this._size; }
}

Given you receive this during debugging, I believe you have a 3rd party control or plugin-in which is causing this exception. Try cleaning/rebuilding, re-adding 3rd party references, and/or running Visual Studio in safe mode.

Upvotes: 1

Related Questions