Program that uses foreach with LINQ query [C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Linq; class Program { static void Main() { // An unsorted string array. string[] letters = { "d", "c", "a", "b" }; // Use LINQ query syntax to sort the array alphabetically. var sorted = from letter in letters orderby letter select letter; // Loop with the foreach keyword. foreach(string value in sorted) { Console.WriteLine(value); } } } Output a b c d |