Best way to check if any value exists in ArrayList

I have an ArrayList . I want to check if any value comes out in t ArrayList . I would like to use any method (from System.Linq namespace), but I can use it only in Array , not ArrayList .

Is there an effective way to test this?

+4
source share
3 answers

Well, you can check .Count > 0 . But the best option would be to stop using ArrayList . Since you know about Any() and System.Linq , I assume that you are not using .NET 1.1; so use List<T> for some T and all your problems will be resolved. It has full use of LINQ-to-Objects, and it is a much better idea.

 List<int> myInts = ... bool anyAtAll = myInts.Any(); bool anyEvens = myInts.Any(x => (x % 2) == 0); // etc 
+8
source

use the link .Cast method to pass a list of arrays to a generic type

 ArrayList ar = new ArrayList(); bool hasItem = ar.Cast<int>().Any( i => i == 1); 
+1
source

Well, you can always use ArrayList.Count, which will give you the number of objects in your array. If its value is 0, there is nothing in it.

0
source

All Articles