C#


// declare an array of type int and add items to it (note: array size is fixed)
int[] numbers = new int[2];
numbers[0] = 100;
numbers[1] = 200;

// create an array and assign values to it
int[] numbers = new int[] { 3, 4, 8, 1, 7, 3, 8, 4 }; // 'new' keyword is optional

// to sort an array of primitive types (e.g. int, string) use Array.Sort
Array.Sort(numbers);

// can also sort an array via LINQ's OrderBy method
int[] sortedNumbers = numbers.OrderBy(x => x).ToArray();

// loop through the array
for (int i = 0; i < numbers.Length; i++)
{
  Console.WriteLine(numbers[i]);
}

// create an array of strings
string[] fruits = { "apple", "banana", "orange" };

// loop through the array using foreach
foreach (string fruit in fruits)
{
  Console.WriteLine(fruit);
}

// ArrayList is a more flexible array but it's recommended to use Lists instead