Advertisement
yoave23

first question

Oct 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.60 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3.     // create a list with 2 elements
  4.     var q1Items = new List<string>()
  5.     {
  6.         "Foo", "Bar"
  7.     };
  8.  
  9.     // aaahhh LINQ will deffer the execution of this query until it's value is actually used (i.e. .ToList())
  10.     var which = q1Items.Where(x => x != "Bar");
  11.  
  12.     // which didn't run yet
  13.     q1Items.Add("Baz");
  14.  
  15.     // q1Items has 3 elements now. the actual where query will happen on the next line
  16.     var nonBar = which.ToList();
  17.  
  18.     // nonBar has 2 items now ("Foo" and "Baz")
  19.     Console.WriteLine(nonBar.Count); // print 2
  20.     Console.ReadKey();
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement