PIRATED 20130914 1710

PIRATED 20130914 1710

NOTE: This content appears to have been plagiarized. Please leave a comment or email tnwiki at Microsoft (with a link to this article) if we are mistaken. The content was pulled from the following source:
The community rules state:
  • "Someone else has relevant content and you want to help them share it with the world. It's a nice thought, but do not copy other people's content to the Wiki, even if the owner said it was OK."



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();
arr.Add(15);
arr.Add(25.35);
arr.Add(10);
arr.Add(20);
arr.Add(25);
arr.Add(20.2);

foreach (var num in arr.OfType<int>().Where(x => x > 10))
{
Console.WriteLine(num);
}

Console.ReadLine();
}

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.

OUTPUT

image


Source

This topic was copied from this DevCurry blog by Suprotim Agarwal.
 

Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Carsten Siemens edited Revision 5. Comment: Pirated Content - see my comment

  • XAML guy edited Revision 4. Comment: added reference, you need to get permission for this

  • Ed Price - MSFT edited Revision 1. Comment: Title casing. Adding tags.

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • As my experience, i think if LINQ is not a solve to all problems, is so close than that. Great job!

  • Ed Price - MSFT edited Revision 1. Comment: Title casing. Adding tags.

  • XAML guy edited Revision 4. Comment: added reference, you need to get permission for this

  • Carsten Siemens edited Revision 5. Comment: Pirated Content - see my comment

  • NOTE: This article was reported as Pirated/Plagiarized Content (content you didn't write) and will be removed. Please do not steal content from others. If you feel we are mistaken, please leave a comment or email tnwiki at Microsoft with a link to this article and with clear and detailed reasons why you own the content or have explicit permission from the author.

    Content was taken from: "Filter a Type in .NET inside the For-Each Loop"

    Published by Suprotim Agarwal on September 20, 2010

    www.devcurry.com/.../filter-type-in-net-inside-for-each-loop.html

Page 1 of 1 (5 items)