What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 36070 |  Welcome, Guest!   Register  Login
 Home > Blogs > LINQ > Difference between While and TakeWhile Extension Method ...
Niladri.Biswas

Difference between While and TakeWhile Extension Method

 Blog author: Niladri.Biswas | Posted on: 5/8/2012 | Category: LINQ Blogs | Views: 834 | Status: [Member] | Points: 75 | Alert Moderator   


Consider the below program

class Program
{
static void Main(string[] args)
{
var intCollection = new List{ 1, 2, 3, 4, 5, -1, -2,-3,-4,-5 };

Console.WriteLine("Where Extension Method");
Console.WriteLine("-----------------------");
intCollection
.Where(i => i <= 4)
.ToList()
.ForEach(i => Console.WriteLine(i));


Console.WriteLine(Environment.NewLine + "TakeWhile Extension Method");
Console.WriteLine("---------------------------");
intCollection
.TakeWhile(i => i <= 4)
.ToList()
.ForEach(i => Console.WriteLine(i));

Console.ReadKey(true);

}
}

The output is

We can infer that Where extension method stops when it satifies all the condition where as TakeWhile extension method exhausts when the first condition trurns out to be false. In other words, Whereextension method examines the whole sequence looking for all the matches but TakeWhile stops when it encounters the first non-match.

Hope this is clear.




Best Regards,
Niladri Biswas
Found interesting? Add this to:


Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, October 25, 2010
Level:Diamond
Status: [Member]
Biography:Lead Engineer at HCL Technologies Ltd., having 6 years of experience in IT field.
I love to explore new technologies and love challenges and try to help others as much as possible not only by coding but also by all possible means.
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 1:12:15 PM