Listnumbers = new List () { 1, 2, 3, 4, 5 }; IEnumerable numberIterator = numbers.GetEnumerator(); while (numberIterator.MoveNext()) { Console.WriteLine(numberIterator.Current); }
class Person { public string Name { get; set; } public int Age { get; set; } } ListThis code defines a Person class with Name and Age properties. It then creates a List of two Person objects and initializes their properties. It uses the Where() and Select() methods from LINQ to filter and project the objects in the List, respectively. Finally, it iterates over the resulting IEnumerable and prints out the names of the adults. The package library for IEnumerable is included in the .NET Base Class Library. It can be used in any C# project that references the System.Collections namespace, which is included by default in most project templates.people = new List () { new Person { Name = "Alice", Age = 24 }, new Person { Name = "Bob", Age = 32 } }; IEnumerable namesOfAdults = people .Where(p => p.Age >= 18) .Select(p => p.Name); foreach (string name in namesOfAdults) { Console.WriteLine(name); }