define array in c# code example

Example 1: c# array

int[] array = new int[]{1, 2, 3, 4, 5};
foreach (string i in array) 
{
  Console.WriteLine(i);
}

//or

for(int i=0; i<array.Length; i++){
	  Console.WriteLine(array[i]);
}

Example 2: c# array

int[] array = new int[]{1, 2, 3, 4, 5};
for(int i=0; i<array.Length; i++){
	  Console.WriteLine(array[i]);
}

Example 3: c# create array

// Define and Initialize
int[] arr = {1, 2, 3};

// Buuuut:
// Initial defining
int[] arr;

// This works
arr = new int[] {1, 2, 3};   

// This will cause an error
// arr = {1, 2, 3};

Example 4: hwo to make an array in C#

int[] Nameofarray = new int[how much is the max];

for example:
int[] X = new int[5];

Example 5: array syntax c#

class TestArraysClass
{
    static void Main()
    {
        // Declare a single-dimensional array of 5 integers.
        int[] array1 = new int[5];

        // Declare and set array element values.
        int[] array2 = new int[] { 1, 3, 5, 7, 9 };

        // Alternative syntax.
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // Declare a two dimensional array.
        int[,] multiDimensionalArray1 = new int[2, 3];

        // Declare and set array element values.
        int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        // Declare a jagged array.
        int[][] jaggedArray = new int[6][];

        // Set the values of the first array in the jagged array structure.
        jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
    }
}

Example 6: c# array

// ------------ How to initialize 1-dimensional Arrays? ------------ //

// 1º Method
int[] myArray = new int[];
myArray[0] = 2;
myArray[1] = 5;
myArray[2] = 9;
  

// 2º Method
int[] myArray = {2, 5, 9};


// 3º Method
int[] myArray = new int[]{2, 5, 9};


// ------------ How to initialize 2-dimensional Arrays? ------------ //

// Rectangular Array
int[,] myRetArray = { {8, 2}, {9, 4} };   // 2 rows and 2 columns
//OR
var myRetArray = new int[3, 5];   // 3 rows and 5 columns
myRetArray[0,1] = 12;


// Jagged Array
var myJagArray = new int[3][];   // 3 rows

myJagArray[0] = new int[2];  // The first row as 2 columns
myJagArray[1] = new int[6];  // The second row as 6 columns
myJagArray[2] = new int[4];  // The third row as 4 columns

// OR
var myJagArray = new int[3][];   // 3 rows

myJagArray[0] = new int[] {7, 8, 4, 6, 1};  // The first row as 5 columns
myJagArray[1] = new int[] {2, 6, 6};       // The second row as 3 columns
myJagArray[2] = new int[] {7, 12, 4, 9, 13, 10, 3};  // The third row as 7 columns

// OR
int[][] myJagArray = new int[][] 
{
    new int[] {1, 4, 3},
    new int[] {9, 2, 3},
	new int[] {2, 5, 9}
};


// -------- How to initialize 3 or more dimensional Arrays? -------- //

var myArray = new int[3, 5, 8];  // 3 rows, 5 columns and 8 cells thick


// -------------- How to creat an Array of Lists? ----------------- //

var my_list = new List<int>[] {
  
	new List<int>() { 9, 2, 1, 2},
	new List<int> { 6, 4, 1, 7},
	new List<int> { 1, 8, 4, 3},
  
};

Console.WriteLine(my_list[0].ElementAt(0));   // You get "9"