int[] numbers = { 1, 2, 3, 4, 5 }; Console.WriteLine("The original array: "); foreach (int i in numbers) { Console.Write(i + " "); } Array.Reverse(numbers); Console.WriteLine("\nAfter reversing the array: "); foreach (int i in numbers) { Console.Write(i + " "); }
Listnames = new List (); names.Add("John"); names.Add("Mary"); names.Add("Bob"); Console.WriteLine("The names in the list are: "); foreach (string s in names) { Console.WriteLine(s); }
int[] numbers = { 1, 2, 3, 4, 5 }; var oddNumbers = numbers.Where(n => n % 2 != 0); Console.WriteLine("The odd numbers in the array are: "); foreach (int i in oddNumbers) { Console.WriteLine(i); }This code demonstrates how to use the Where() extension method from the System.Linq package to filter an array of integers and return only the odd numbers.