LINQ in C#
Hello guys, sorry to disturb you all; I'm currently learning about arrays. I noticed that we can query the array to return a new one using the following syntax:
evenNumbers = numbers.Where(n => n % 2 == 0).ToArray();
I have one question. I understood that the ToArray() method is used to convert the filtering result into an array. But what if we omit it? What would happen? What would evenNumber store? Would we be able to access the filtered values?4 Replies
it would store an
IEnumerable<int>
that represents the sequence produced by the filter
you could use that directly in a foreach
loop for example and the resulting sequence would be calculated on demandI read that this is also known as a "lazy sequence"
What do we mean by lazy sequence pls
it means that it's only evaluated when you ask for the result
for example, calling
numbers.Where(n => n % 2 == 0)
doesn't actually do any filtering
the filtering happens when you ask for the results, like using it in a foreach or calling ToArrayyep I see
Thanks !