ARRAY

C sharpArrays in c sharp are collections of objects all of the same type. An array is declared with a pair of square brackets following the type definition.

int[] x;

An array in c sharp has a fixed size, once the number of elements it contains has been declared it can no longer be changed.

DECLARATION OF AN ARRAY

int[] x = new int[10];

we have declared an array that contains 10 elements, the index of an array starts from zero. To assign a value to an array, the following syntax is used, remembering that the index starts from zero.

x[2] = 50;

We assigned the value 50 to the third element of the array.

int y = x[2]

Read access to the third element of the array.

The following syntax is used to initialize all elements of an array:

int[] x = new int[10] {1,2,3,4,5,6,7,8,9,10}

You can use type.

int[] x = {1,2,3,4,5,6,7,8,9,10}

the compiler deduces this information from the literal, ie from the number and type of values contained in the curly brackets.

ARRAY IN C SHARP MULTIDIMENSIONAL ARRAYS

  • JAGGED ARRAY

int [][] a = new int[3][]

a[0] = new [] {1,2,3};

a[1] = new [] {4,5};

a[2] = new [] {6,7,8,9};

Array of array, has 3 elements of type array each of which has dimension 3, 2, 4. An equivalent form to the above declaration is the following:

int [][] a = {

                    new [] {1,2,3},

                    new [] {4,5},

                    new [] {6,7,8,9}

                   };

int x = [2][2]   we access the value 8.

If we know unlike jagged arrays that all arrays have the same size then we are talking about rectangular or multidimensional arrays.

  • RECTANGULAR ARRAYS

int [ , ] = new int[3,3]; two-dimensional array

int [ , ] a = {

   {1,2,3},

   {4,5,6},

   {6,7,8}

};

int x = a[1,2]  we access the value 6.

using System;
namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
           int[] x = new []{1,2,3,4,5,6,7,8,9,10};
           foreach(var item in x)
           {
               Console.WriteLine(item);
           }
           /*-ARRAY RECTANGULAR 5 BY 5 WHERE EVERY FIRST COLUMN
              MUST BE SET TO 0 */
           int[,] array = new int[5,5];
           int counter = 0;
           for(int y = 0; y < 5; y++)//loop in rows
           {
               for(int z= 0; z < 5; z++)//loop in columns
               {
                   ++counter;
                   array[y,z] = (z==0 ? 0 : counter);
               }
           }
           for(int y = 0; y < 5; y++)//loop in rows
           {
               for(int z= 0; z < 5; z++)//loop in columns
                   Console.Write($"{array[y,z]}");
               Console.Write("\n");     
           }
        }
    }
}

DEEPENING

Arrays in C# are a data structure for storing a collection of elements of the same type. Arrays can be one-dimensional (one-dimensional), two-dimensional, or multi-dimensional. Here is an overview of how to work with arrays in C#.

Array Declaration and Initialization

One-Dimensional Arrays

To declare a one-dimensional array in C#, the following syntax is used:

int[] numeri; // dichiarazione
numeri = new int[5]; // inizializzazione con dimensione

// oppure
int[] numeri = new int[5]; // dichiarazione e inizializzazione

// oppure
int[] numeri = { 1, 2, 3, 4, 5 }; // inizializzazione con valori

Two-Dimensional Arrays

Two-dimensional arrays are used to represent tables or matrices:

int[,] matrice = new int[3, 3]; // dichiarazione e inizializzazione di una matrice 3×3

// oppure
int[,] matrice = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // inizializzazione con valori

Accessing Array Elements

Array elements can be accessed using indexes:

numeri[0] = 10; // assegna 10 al primo elemento
int primoElemento = numeri[0]; // legge il primo elemento

// Accesso agli elementi in un array bidimensionale
int valore = matrice[1, 2]; // accede all’elemento nella seconda riga e terza colonna
matrice[2, 0] = 42; // assegna 42 all’elemento nella terza riga e prima colonna

Iteration on Array Elements

You can iterate over the elements of an array using for or foreach loops:

// Iterazione su un array monodimensionale
for (int i = 0; i < numeri.Length; i++)
{
      Console.WriteLine(numeri[i]);
}

// Iterazione su un array bidimensionale
for (int i = 0; i < matrice.GetLength(0); i++)
{
   for(int j = 0; j < matrice.GetLength(1); j++)
   {
     Console.WriteLine(matrice[i, j]);
   }
}

Useful Methods for Arrays

C# provides several useful methods for working with arrays, such as:

Array.Sort(array): Sorts the elements in the array.

Array.Reverse(array): Reverses the order of the elements in the array.

Array.IndexOf(array, value): Returns the index of the first element corresponding to the specified value.

Full Example

using System;

class Program
{
    static void Main()
    {
       // Dichiarazione e inizializzazione
       int[] numeri = { 1, 3, 5, 7, 9 };

       // Accesso agli elementi
       Console.WriteLine(“Primo elemento: ” + numeri[0]);

       // Modifica di un elemento
       numeri[1] = 4;

       // Iterazione con ciclo for
       Console.WriteLine(“Elementi dell’array:“);
       for (int i = 0; i < numeri.Length; i++)
       {
           Console.WriteLine(numeri[i]);
       }

       // Utilizzo di Array.Sort
       Array.Sort(numeri);
       Console.WriteLine(“Array ordinato:“);
       foreach (int num in numeri)
       {
           Console.WriteLine(num);
       }
    }
}

This guide provides a basic overview of arrays in C#, including declaration, initialization, element access, and some common operations.

LINK TO PREVIOUS POST

LINK TO THE CODE ON GITHUB