C# Arrays

Create an Array

Arrays are used to store multiple values ​​for a single variable, instead of declaring different values ​​for each value.

To declare an array, define the variable type with square brackets:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
        

We have now declared the variable that holds the array of strings.

To add values ​​to it, we can use the array literal - placing values ​​in a comma-separated list, within curly braces:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
        

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};
        

Access the Elements of an Array

You are accessing an element of the same members by referring to the index number.

This statement accesses the value of the first element in cars

Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            Console.WriteLine(cars[0]);
            // Outputs Volvo
            

Change an Array Element

To change the value of a specific element, see the index number:

Example
cars[0] = "Opel";
        
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            cars[0] = "Opel";
            Console.WriteLine(cars[0]);
            // Now outputs Opel instead of Volvo
            

Array Length

To find out how many elements of the same array, use the length feature:

Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            Console.WriteLine(cars.Length);
            // Outputs 4
            

Loop Through an Array

You can loop between elements of the for loop with a loop, and use the Length feature to specify how many loops should work.

The following example removes all elements from the cars array:

Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            for (int i = 0; i < cars.Length; i++) 
            {
              Console.WriteLine(cars[i]);
            }
            

The foreach Loop

There is also a foreach loop, which is only used to combine features in the array:

Syntax
foreach (type variableName in arrayName) 
            {
              // code block to be executed
            }
            

The following example outputs all elements from the cars array, using a foreach loop:

Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            foreach (string i in cars) 
            {
              Console.WriteLine(i);
            }
            

The example above can be read as follows: for each string (called i - as a reference) in cars, print the value of i.

If you compare the for loop with the foreach loop, you will see that the foreach method is easy to write, does not require a counter (using Length property), and is very readable.


Sort Arrays

There are many similar member modes available, for example Sort(), which filters the list in alphabetical order or in ascending order:

Example
// Sort a string
            string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            Array.Sort(cars);
            foreach (string i in cars)
            {
              Console.WriteLine(i);
            }
            
// Sort an int
            int[] myNumbers = {5, 1, 8, 9};
            Array.Sort(myNumbers);
            foreach (int i in myNumbers)
            {
              Console.WriteLine(i);
            }

System.Linq Namespace

Other similar helpful array methods, such as Min, Max, and Sum, can be found in System.Linq namespace:

Example
using System;
            using System.Linq;
            
            namespace MyApplication
            {
              class Program
              {
                static void Main(string[] args)
                {
                  int[] myNumbers = {5, 1, 8, 9};
                  Console.WriteLine(myNumbers.Max());  // returns the largest value
                  Console.WriteLine(myNumbers.Min());  // returns the smallest value
                  Console.WriteLine(myNumbers.Sum());  // returns the sum of elements
                }
              }
            }
            

Other Ways to Create an Array

If you are familiar with C #, you may have seen arrays created with a new keyword, and you may have seen arrays of a certain size. In C #, there are different ways to create a list:

// Create an array of four elements, and add values later
            string[] cars = new string[4];
            
            // Create an array of four elements and add values right away 
            string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};
            
            // Create an array of four elements without specifying the size 
            string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"};
            
            // Create an array of four elements, omitting the new keyword, and without specifying the size
            string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            

It's up to you to decide. In our study, we will often use the latter option, as it is quick and easy to read.

However, you should be aware that when you announce the list and launch it later, you should use a new keyword:

// Declare an array
            string[] cars;
            
            // Add values, using new
            cars = new string[] {"Volvo", "BMW", "Ford"};
            
            
// Add values without using new (this will cause an error) cars = {"Volvo", "BMW", "Ford"};