C
C#6d ago
Faker

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
Jimmacle
Jimmacle6d ago
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 demand
Faker
FakerOP6d ago
I read that this is also known as a "lazy sequence" What do we mean by lazy sequence pls
Jimmacle
Jimmacle6d ago
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 ToArray
Faker
FakerOP6d ago
yep I see Thanks !

Did you find this page helpful?