using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace CSharpCollectionArrayList { class Program { static void Main(string[] args) { //Initialize an arrayList (Collection) ArrayList intArray = new ArrayList(); // Populate the arraylist //Add 5 elements to the array list "intArray" //The elements are: 10, 20, 30, 40 and 50 for (int i = 0; i < 5; i++) { intArray.Add((i + 1) * 10); } //Print each member of the ArrayList //the foreach is an enhanced for loop. It has the same purpose of the for loop // For each element i in the arraylist Console.WriteLine("Displaying the contents of the array list "); foreach (int i in intArray) { Console.Write("{0} ", i.ToString()); } // Reverse the order of the items in the list and display them // using the "Reverse method" intArray.Reverse(); Console.WriteLine("\n\nNow the list is reversed "); //Print the reversed array foreach (int i in intArray) { Console.Write("{0} ", i.ToString()); } // Sort the items in the list and display them intArray.Sort(); Console.WriteLine("\n\nNow the order of the list has been sorted "); //Print the sorted array foreach (int i in intArray) { Console.Write("{0} ", i.ToString()); } // Clear the items in the list and display them // Clear removes all the elements from the array list intArray.Clear(); Console.WriteLine("\n\nNow the list has been cleared "); if ((intArray.Count) == 0) { Console.WriteLine("There are no longer any items in this list"); } System.Console.ReadKey(); } } }