Lately I am interested in C# again and thus I have taken a deeper look at LINQ (Language Integrated Query). My previous article, concerning it was here.
I thought it was interesting, that the value in LINQ is calculated after it is used, and not after it’s time to be calculated.
Thus, if you take a look at the following simple example, you would see that col1 should not be containing the value of 1000, because it is added later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace linqMe { class linqMe { static void Main() { List numbers = new List { 5,6,7,8,9,20}; var col1 = from n in numbers where n > 6 select n; numbers.Add(1000); foreach (var n in col1) { Console.WriteLine(n); } } } } |
However, if you simply run it, you get this:
And thus, you would see the 1000 inside. That’s how LINQ works! 🙂