C# – LINQ – Example for LINQ initialization

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. cs

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.

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:
linqA

And thus, you would see the 1000 inside. That’s how LINQ works! 🙂