Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting specific object types in an ArrayList C#

Tags:

c#

arraylist

how does one go about counting the amount of a specific object type in an ArrayList in C#?

More specifically, I have three subclasses of base class 'Letter', 'X', 'Y' and 'Z'. Various amounts of X, Y and Z objects have been created and added to an arraylist. I then need to count how many X objects are in that list. What's the best way of going about this?

Cheers for any help guys/gals.

like image 404
Ari Avatar asked Dec 07 '22 18:12

Ari


1 Answers

You can also use the OfType<T> Extension Method:

int myCount = myArrayList.OfType<X>().Count()
like image 156
nemesv Avatar answered Dec 28 '22 22:12

nemesv