0
0
C Sharp (C#)programming~10 mins

Array as reference type behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array as reference type behavior
Create array object in heap
Variable holds reference to array
Assign variable to another variable
Both variables point to same array
Change array via one variable
Change visible via other variable
Arrays in C# are stored in the heap. Variables hold references to the array object. Assigning one variable to another copies the reference, not the array. Changes via one variable affect the same array seen by the other.
Execution Sample
C Sharp (C#)
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 10;
Console.WriteLine(arr1[0]);
This code shows that changing the array through arr2 also changes arr1 because both refer to the same array.
Execution Table
StepActionarr1arr2Array ContentOutput
1Create arr1 with {1,2,3}ref#100null[1, 2, 3]
2Assign arr2 = arr1ref#100ref#100[1, 2, 3]
3Set arr2[0] = 10ref#100ref#100[10, 2, 3]
4Print arr1[0]ref#100ref#100[10, 2, 3]10
💡 Program ends after printing value 10, showing arr1 reflects change made via arr2.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arr1nullref#100ref#100ref#100ref#100
arr2nullnullref#100ref#100ref#100
Array ContentN/A[1, 2, 3][1, 2, 3][10, 2, 3][10, 2, 3]
Key Moments - 3 Insights
Why does changing arr2[0] also change arr1[0]?
Because arr1 and arr2 both hold references to the same array object (see execution_table step 2). Changing the array via arr2 changes the single shared array.
Does assigning arr2 = arr1 create a new array?
No, it copies the reference only. Both variables point to the same array in memory (execution_table step 2).
What would happen if we changed arr2 to a new array?
Then arr2 would reference a different array, and changes to arr2 would not affect arr1. This is not shown here but is important to understand.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the content of the array?
A[1, 2, 3]
B[10, 2, 3]
C[0, 2, 3]
D[10, 10, 3]
💡 Hint
Check the 'Array Content' column at step 3 in execution_table.
At which step do arr1 and arr2 first point to the same array reference?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'arr1' and 'arr2' columns in execution_table to see when they share the same reference.
If we changed arr2 to a new array after step 2, what would happen to arr1?
Aarr1 would become null
Barr1 would also change to the new array
Carr1 would remain unchanged
Darr1 would throw an error
💡 Hint
Remember that assigning a new array to arr2 changes only arr2's reference, not arr1's.
Concept Snapshot
Arrays in C# are reference types.
Variables hold references to arrays in heap.
Assigning one array variable to another copies reference.
Changes via one variable affect the same array.
To avoid shared changes, create a new array copy.
Full Transcript
In C#, arrays are stored in the heap and variables hold references to them. When you create an array and assign it to a variable, that variable points to the array object. If you assign that variable to another variable, both variables point to the same array. Changing the array through one variable changes it for the other as well. This is because only the reference is copied, not the array itself. To have independent arrays, you must create a new array object.