I have ArrayList
.
I want to check if any value exits in t his ArrayList
.
I would like to use Any method (from System.Linq
namespace), but I can use it only on Array
, not ArrayList
.
Is there any efficient way to check this?
Well, you could check the .Count > 0
. But a better option would be stop using ArrayList
. Since you know about Any()
and System.Linq
, I assume you're not using .NET 1.1; so use List<T>
for some T
, and all your problems will be solved. This has full LINQ-to-Objects usage, and is just a much better idea.
List<int> myInts = ...
bool anyAtAll = myInts.Any();
bool anyEvens = myInts.Any(x => (x % 2) == 0);
// etc
use the link .Cast
method to cast your array list to a generic type
ArrayList ar = new ArrayList();
bool hasItem = ar.Cast<int>().Any( i => i == 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With