How to Prove Array as a Reference type variable ?

 

As, we know that variables like int, float, double etc. are value type and Array is a reference type variable. To, prove this by a program here we go:

 

1: let define and declare 2 different array and name them according to you, I’ve given them name as array1 and array2.

2: I’ve put their values as {1,2,3} for first and {10,20,30} for second.

3: Check their values through message box.

4: Now Let’s assign the value of array2 to array1.

5: Now, explicitly change the value of 1st element of array1.

6: Now, check the value of 1st element of Array2.

 

and you’ll see that the value of array2 is changed by default and this is a property of reference type.

 

Thanks,

Source Code:

namespace To_Prove_Array_As_a_Reference

{

public partial class FrmArrayAsaReference : Form

{

public FrmArrayAsaReference()

{

InitializeComponent();

}

private void btnonclick_Click(object sender, EventArgs e)

{

int[] array1 = { 1, 2, 3 };

int[] array2 = { 10, 20, 30 };

array1 = array2;

MessageBox.Show(Convert.ToString(array1[1]));

MessageBox.Show(Convert.ToString(array2[1]));

array1[1] = 100;

MessageBox.Show(Convert.ToString(array2[1]));

}

}

}

Output2 output