Here’s a nice way to filter an ArrayList right inside the For-Each Loop. We have to filter and print only ‘integers’ greater than 10.
static void Main(string args)
ArrayList arr = new ArrayList();
foreach (var num in arr.OfType<int>().Where(x => x > 10))
As you can observe, we are using the Enumerable.OfType<TResult> inside the foreach loop to filter only those elements of the ArrayList which are integers and greater than 10. This code will only work in .NET 3.5 and above.
Will you give this article a +1 ? Thanks in advance