How to print limited range of the List<int>
Page 1 of 114 Replies - 250 Views - Last Post: 26 September 2012 - 01:48 PM
#1
How to print limited range of the List<int>
Posted 26 September 2012 - 07:54 AM
Replies To: How to print limited range of the List<int>
#2
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:01 AM
List<Int32> fop = new List<int>();
fop.Add(1);
fop.Add(2);
Console.WriteLine(fop[0]);
Console.WriteLine(fop[1]);
#3
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:05 AM
Anyways thanks for the help
#4
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:09 AM
#5
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:12 AM
#6
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 08:52 AM
list.Take(7).ForEach(item => Console.WriteLine(item))
This post has been edited by ThrowsException: 26 September 2012 - 08:54 AM
#7
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 09:03 AM
#8
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 10:08 AM
#9
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 12:26 PM
#10
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 12:39 PM
Julyuary, on 26 September 2012 - 05:08 PM, said:
Because it is shorter........
Doesn't Take make a new List and iterate through the first 7 items of the original list to put them in then new one? And then you iterate over the items again to print them out
#11
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:12 PM
CasiOo, on 26 September 2012 - 02:39 PM, said:
I believe this would only happen if you were to say
list.Take(7).ToList()
If you don't add the ToList you never actually materialize the data until you hit the ForEach() and start actually using the data.
Someone please correct me if I'm mistaken about this.
#12
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:23 PM
First of all, the LINQ in question won't even compile. ForEach isn't defined in the LINQ libraries, and it isn't an extension method to IEnumerable<T> like the rest of the LINQ methods are.
.ForEach is an instance method defined on the List<T> class itself. It only works on Lists.
Since .Take returns an IEnumerable, this won't work. If one were to call .ToList on the result of .Take, then it would work, but that would be inefficient, because .ToList creates a new list from the IEnumerable it is called on.
Quote
No, Take does not make a new list. IEnumerables are lazily evaluated. Basically, each extension method adds to the instructions that can create the sequence, but are not actually evaluated until necessary. So, assuming that the ForEach would actually work on this (which we've already established it won't), .Take wouldn't create a new list, it would create an IEnumerable that knows how to get the data it needed. Then, in the ForEach, as each value was needed, it would be enumerated and used.
LINQ can be quite efficient, if you understand how it works. For something as simple as looping through a few values, it really doesn't matter how you do it. Simple is usually better than clever though.
#13
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:35 PM
for (var i = 0; i < 7; i++)
{
Console.WriteLine(myList[i]);
}
#14
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:39 PM
#15
Re: How to print limited range of the List<int>
Posted 26 September 2012 - 01:48 PM
|
|

New Topic/Question
Reply



MultiQuote









|