Common Bug #23

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

bug2.JPG

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,

bug3.JPG

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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: