C#. Arrays. Part 3. References to arrays. The Length property. Assigning arrays

Arrays. Part 3. References to arrays. The Length property. Assignment of arrays


Search other websites:

1. How is realized the assignment of references to the array? How does the C# compiler implement the assignment of variables of type “array”? Example

For any newly created array, memory is allocated using the new operator. This means that the arrays are of a reference type (rather than a value type). That is, variables of type “array” is a reference to the array.

After assigning the value of one variable (array reference) to another variable, both variables will point to (refer to) the same array. That is, a copy of the array is not created.

For variables to point to different parts of the memory (different data sets) – for any of them, you need to allocate memory using the new operator. If you need to copy data from one array to another, then you need to do this programmatically, for example, using the loop statements.


2. Example of assigning the variables to array references

Example. In the example, an array of 20 real numbers is declared and the operation of assigning references to this array is demonstrated.

// copying references to arrays
double[] M1 = new double[20]; // declaration of an array M1 of 20 real numbers
double[] M2; // for the M2 array, no memory is allocated

// filling the array M1 arbitrary values
for (int i = 0; i < 20; i++)
    M1[i] = 0.1 * i;

M2 = M1; // Now M2 and M1 refer to the same memory location

double d;
d = M2[3]; // d = 0.3


3. How to determine the number of items in an array? The Length property

The length (number of items) of the array can be stored in the program in special, additional variables. Since, in C#, an array is an object, its length is stored in the Length property.

// demonstration of using the Length property
char[] C = new char[20];
float[] F = new float[100];
int[] I = new int[500];
int d;
d = C.Length; // d = 20
d = F.Length; // d = 100
d = I.Length; // d = 500


4. What are the features of applying the Length property to multidimensional arrays?

In the case of multidimensional arrays, the Length property returns the number of items of the entire array.

// Length property for multidimensional arrays
uint[,] UI = new uint[200,10]; // two dimensional array
bool[][] B = new bool[3][]; // stepped array
float[, ,] F = new float[3, 4, 5]; // three dimensional array

B[0] = new bool[10];
B[1] = new bool[15];
B[2] = new bool[20];

int d;
d = UI.Length; // d = 200*10 = 2000
d = B[0].Length; // d = 10
d = B[1].Length; // d = 15
d = B.Length; // d = 3
d = F.Length; // d = 3*4*5 = 60


5. An example of calculating the sum of array elements using the Length property
// sum of items of a one-dimensional array
int[] A = new int[10]; // array declaration

// array filling with arbitrary values
for (int i = 0; i < A.Length; i++)
    A[i] = i;

// calculating the sum of array items
int s = 0;

for (int i = 0; i < A.Length; i++)
    s = s + A[i];
// s = 45


6. How can I apply the Length property to stepped arrays?

For stepped arrays, the Length property can be used for the whole array as well as for each array, which is a row. In the first case, the Length property returns the number of rows in the array. In the case of each row, the Length property returns the length of each row.

// Length property for stepped arrays
int[][] A = new int[5][];

A[0] = new int[10];
A[1] = new int[20];
A[3] = new int[25];

int d, d1, d2, d3;

d = A.Length; // d = 5;
d1 = A[0].Length; // d = 10
d2 = A[1].Length; // d = 20

//d3 = A[4].Length; - error, there is no reference to A[4]


Related topics