Arrays

Arrays are the most basic collection variable available in .NET Framework. Arrays are used to store a group of data variables that are of the same type as int, String, and many more. 

Let's go through a code implementation in which we will create an array collection of int variables and then loop through them:

public static void CollectionOperations()
{
int[] arrayOfInt = new int[10];
for (int x = 0; x < arrayOfInt.Length; x++)
{
arrayOfInt[x] = x;
}
foreach (int i in arrayOfInt)
{
Console.Write(i);
}
}

Please note the following in the code implementation:

  • An array is declared by the [] syntax after the data type. In the implemented code, we are declaring an array of the int type.
  • While declaring the array collection, we must define the length of the array collection. In the code, we are declaring that the length of the array is 10. This basically implies that the array can contain 10 elements of the int type.
  • The index of the array starts at 0. This implies that, in the preceding case, the first element will start at index 0 and will end at index 9. If we need to refer to the elements present in the ith index in the array, we can use the array[i] declaration.
  • Each array has a length property. This attribute indicates the maximum number of elements that can be present in the array. In the code, we are doing a for loop from 0 to 9 that is of the length -1 and are setting the value in each element present in that index.
  • Array implements the IEnumerable interface. Due to this, we can loop through the array using the foreach loop.

On executing the preceding code, we get the following output:

In the code, we are setting values in each index of the array—the same value as the index in itself. Before we move ahead and start looking at the code examples, we need to understand two important concepts regarding arrays:

  • Multidimensional arrays: These are arrays in which we store elements in a matrix structure that has both rows and columns. For example, we can use the following code for declaring a multidimensional array:
int[,] arrayInt = new int[3,2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
  • The use of a single , mark during the array declaration indicates that this is a two-dimensional array. In the case of a two-dimensional array, the first dimension indicates the number of rows, while the second indicates the number of columns present in the array. Also, note that by using 3,2, we are basically indicating that the number of rows in the array should be 3 while the number of columns should be 2.

Just like in a normal array, in a multidimensional array, the index for both dimensions starts at 0. Therefore, the first element will be present at index {0,0}, while the last element will be present at index {2,1}

Let's look at the following code implementation, in which we will look at the code to loop through this multidimensional array and read each number present in the array:

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

In the preceding code implementation, we are doing two nested loops. In the first loop, we are looping through the number of rows present in the multidimensional array. In the second loop, we are looping through the number of columns present in the multidimensional array. Using the arrayInt[i, j] syntax, we are printing the element present in that combination of rows and columns in the array. 

When we execute the code, we get the following result. As we have to loop through the two-dimensional array using a nested loop, at each step, we will access elements present at position {i, j}. For each iteration of the i variable, the j variable will iterate from 0 to 1. Hence, it will start at {0,0}, which will be 1 and will end at {2,1}, which will be 6, hence generating the following output:

In the next section, we will look at another collection type: lists.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.66