Listnumbers = new List { 1, 2, 3, 4, 5 }; List squares = numbers.Select(x => x * x).ToList(); // squares now contains { 1, 4, 9, 16, 25 }
ListIn this example, we create a list of strings and then use the Select method to generate a new list that contains a personalized greeting for each name in the list. The System.Collections.Generic namespace and the Listnames = new List { "John", "Mary", "Tom" }; List greetings = names.Select(name => $"Hello, {name}!").ToList(); // greetings now contains { "Hello, John!", "Hello, Mary!", "Hello, Tom!" }