CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<int> list1 = new List<int>() {1,2,3};
List<int> list2 = new List<int>() {1,2,3};
var result = list1.Select(x => x);
// Appened to the _same_ list.
list1.Add(12);
// Prints: 1, 2, 3, 12.
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.WriteLine("----");
// Results are enumerated, in this scenario.
var result2 = list2.Select(x => x).ToList();
// Appending to the list has no impact on result2, because it's been enumerated.
list2.Add(69);
// Prints: 1, 2, 3
foreach (var item in result2)
{
Console.WriteLine(item);
}
}
}