Replace arrays with C # variables

S Fattahi
5 min readJul 19, 2020

An array stores a set of the same length of data of the same type. An array is used to store a set of data. But the array can be used as a set of variables of the same type in the house. Looked at bursts stored in memory.
Instead of defining separate variables, such as number0, number1, etc., an array of variables called numbers can be defined. And to use each cell of numbers [0], numbers [1] and … used. A specific element in an array will be accessible by its index.
An array consists of consecutive cells in memory. The smallest address is for the first cell of the array and the largest address is for the last cell.

Array definition:
The following command is used to define an array in C #:

datatype[] arrayName;

datatype: Specify the type of data to be stored in the array

[ ]: Specifies the length of the array

arrayName: Is the name of the array

Array value:
An array can also be set by default. This is possible using the new keyword. The following example shows three methods for determining and quantifying arrays:

// defining array with size 5. add values later on

int[] intArray1 = new int[5];

// defining array with size 5 and adding values at the same time

int[] intArray2 = new int[5]{1, 2, 3, 4, 5};

// defining array with 5 elements which indicates the size of an array

int[] intArray3 = {1, 2, 3, 4, 5};

In the example above, the first expression first defines an array of int data type that can store 5 integer values. The second expression, like the first expression, performs the definition operation and then initializes with values.
The third expression performs quantitative activity without specifying the number of array cells. In this case, the array size is automatically subtracted from the number of values. If we perform the quantification operation without specifying the array size (example below), we will encounter a compiler error. The following example creates an error when collecting:

int[] intArray = new int[]; // compiler error: must give size of an array

Value after definition:
Arrays can be initialized after definition. It is not necessary to define and initialize at the same time. Consider the following example:

string[] strArray1, strArray2;

strArray1 = new string[5]{ “1st Element”,

“2nd Element”,

“3rd Element”,

“4th Element”,

“5th Element”

};

strArray2 = new string[]{ “1st Element”,

“2nd Element”,

“3rd Element”,

“4th Element”,

“5th Element”

};

However, you must use a new keyword when using this method. In this case, quantification can not be done by assigning values alone. The following example encounters an error:

string[] strArray;

strArray = {“1st Element”,”2nd Element”,”3rd Element”,”4th Element” };

Access the elements of an array:
1. An element will be accessible by array indexes. To do this, simply insert the index of the array into its variable bracket. Consider the following example.

double salary = test[9];

With the above command, the information about box number 10 (index 9) will be provided and stored in the salary variable.
Consider the following example: In this example, we want to display the information of each component of an array with 10 cells. Note that when defining int arrays, the initialization is such that initially all arrays are 0. In this example, each device is added to the number 100 and the resulting value is printed along with their index display:

using System;

namespace ArrayApplication

{

class MyArray

{

static void Main(string[] args)

{

int [] n = new int[10]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n */

for ( i = 0; i < 10; i++ )

{

n[ i ] = i + 100;

}

/* output each array element’s value */

for (j = 0; j < 10; j++ )

{

Console.WriteLine(“Element[{0}] = {1}”, j, n[j]);

}

Console.ReadKey();

}

}

}

Example output:

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

2.Use the foreach ring:

The foreach loop means “for each”. This loop is used to access individual elements and items of an array to avoid repeating a command. Consider the following example:

using System;

namespace ArrayApplication

{

class MyArray

{

static void Main(string[] args)

{

int [] narray = new int[5] {1,2,3,4,5}; /* n is an array of 10 integers */

foreach ( int i in narray)

{

Console.WriteLine(“Element = {0}”, i);

}

Console.ReadKey();

}

}

}

In this case, each layer of an array is printed and the output will be as follows:

Element = 1

Element = 2

Element = 3

Element = 4

Element = 5

Multidimensional arrays:
In the previous examples, the arrays we created were variables that included multiple cells. These arrays are also called linear arrays. But suppose we want to store a 9-by-9 multiplication table in an array. To do this, we must use multidimensional arrays. For example, the following command defines a 2D array consisting of 9 rows and 9 columns:

int[,] martix = new int[9,9];

For more dimensions we need to increase the number of numbers inside []:

int[,,] martix = new int[9,9,9];

In the following example, we define a 9-by-9 array and store the 9-by-9 multiplication table inside it, and finally print the output:

int[,] matrix = new int[9,9];

for (int col = 0; col < 9; col++)

{

for (int row = 0; row < 9; row++)

{

matrix[col, row] = (col + 1)*(row + 1);

}

}

for (int col = 0; col < 9; col++)

{

for (int row = 0; row < 9; row++)

{

Console.Write(matrix[col, row] + “\t”);

}

Console.WriteLine();

}

Jagged Arrays:
In the previous section, we were introduced to multidimensional arrays, such as 2 ** 2 or 3 ** 3 or 2 ** 4 arrays, but suppose we want to define a two-dimensional array with an unequal number of cells. Next, each row of the array had a certain number of cells, such as 3 rows and 2 columns, but suppose we want each row of the array to have a different number of cells. For example, the first row is 5 cells, the second row is 6 cells, the third row is 2 cells, and so on. In such cases, we can use irregular arrays. The definition of irregular arrays is as follows:

int[][] jaggedArray = new int[3][];

With the above command, we define a three-cell array, each cell of which is also an array. Now we need to create an array for each of the array cells as well:

jaggedArray[0] = new int[5];

jaggedArray[1] = new int[4];

jaggedArray[2] = new int[2];

You can also value each of the array cells directly, as in the following example:

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };

jaggedArray[1] = new int[] { 0, 2, 4, 6 };

jaggedArray[2] = new int[] { 11, 22 };

In the example above, we created a 3-cell array where the first cell has 5 cells, the second cell has 3 cells, and the third cell has 2 cells.
To access array cells, two indices must be used, the first index for the first array and the second index for the second array:

jaggedArray[0][1] = 77;

Console.WriteLine(“{0}”, jaggedArray[0][1]);

last word:
Well, snake friends, we told you everything you needed to know about arrays in C #, but if you want to learn it once and for all, you can use our practical C # tutorials. Thank you….

--

--

S Fattahi

Web designer Isolate music and writing Bachelor of Computer Software