Common Bug #23

A common bug occurs when we need to check if a collection has at least one element.


Note that if there are no elements in the collection (ArrayList) an ArgumentOutOfRangeException is thrown (compared to an IndexOutOfRangeException).

The correct way to check would be to use,


Note that you should also check that myArrayList is not null before accessing the Count property.


About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: