Arrays in C# are special because they are stored as references. This means when you change an array through one variable, other variables pointing to the same array see the change too.
Array as reference type behavior in C Sharp (C#)
using System; class Program { static void Main() { int[] originalArray = {1, 2, 3}; int[] referenceToArray = originalArray; // Both variables point to the same array referenceToArray[0] = 10; // Change affects originalArray too Console.WriteLine(originalArray[0]); // Prints 10 } }
Arrays are reference types in C#, so variables hold references (addresses) to the actual array data.
Assigning one array variable to another copies the reference, not the array contents.
int[] emptyArray = new int[0]; int[] anotherReference = emptyArray; anotherReference = new int[5]; // Now points to a new array, original emptyArray unchanged
int[] singleElementArray = {5}; int[] referenceCopy = singleElementArray; referenceCopy[0] = 20; // singleElementArray[0] is now 20 too
int[] array = {1, 2, 3}; int[] reference = array; reference[2] = 99; // array[2] is now 99
This program shows that changing the array through one variable changes it for the other because both point to the same array.
using System; class Program { static void PrintArray(string label, int[] array) { Console.Write(label + ": "); foreach (int item in array) { Console.Write(item + " "); } Console.WriteLine(); } static void Main() { int[] originalArray = {1, 2, 3}; Console.WriteLine("Before change:"); PrintArray("originalArray", originalArray); int[] referenceToArray = originalArray; // reference copy referenceToArray[1] = 20; // change through reference Console.WriteLine("After change through referenceToArray:"); PrintArray("originalArray", originalArray); PrintArray("referenceToArray", referenceToArray); } }
Time complexity: Access and modification by index is O(1).
Space complexity: No extra space used when copying references.
Common mistake: Expecting that assigning one array variable to another copies the array contents, but it only copies the reference.
Use this behavior when you want multiple parts of your program to work on the same array data without copying.
Arrays in C# are reference types, so variables hold references to the actual array.
Assigning one array variable to another copies the reference, not the array itself.
Changing the array through any reference changes the original array data.
Practice
int[] a = {1, 2}; int[] b = a;?Solution
Step 1: Understand array assignment in C#
Arrays are reference types, so assigning one array variable to another copies the reference, not the data.Step 2: Effect of assignment on variables
Both variables point to the same array object in memory, so changes via one affect the other.Final Answer:
Both variables refer to the same array in memory. -> Option AQuick Check:
Array assignment copies reference, not data [OK]
- Thinking assignment copies array elements
- Assuming a new array is created automatically
- Confusing arrays with value types
Solution
Step 1: Review array declaration syntax
In C#, arrays are declared with the type followed by square brackets, and initialized withnewand size or values.Step 2: Check each option
int[] arr = new int[3] {5, 10, 15}; uses correct syntax with size and initializer. int arr = {5, 10, 15}; misses array brackets. int[] arr = new int {5, 10, 15}; misses the square brackets afterint. int arr[] = new int[] {5, 10, 15}; uses C-style syntax which is invalid in C#.Final Answer:
int[] arr = new int[3] {5, 10, 15}; -> Option BQuick Check:
Correct array declaration usesnew int[size] {values}[OK]
int[] arr = new int[size] {values}; for arrays [OK]- Omitting square brackets in declaration
- Using C-style array syntax
- Missing
newkeyword with size
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 10;
Console.WriteLine(a[0]);Solution
Step 1: Understand reference assignment
Variablebis assigned the reference of arraya, so both point to the same array.Step 2: Effect of modifying
Changingb[0]b[0]to 10 changes the first element of the shared array.Step 3: Output of
Sincea[0]aandbreference the same array,a[0]is now 10.Final Answer:
10 -> Option DQuick Check:
Arrays share reference, so change reflects in both [OK]
- Expecting original array to remain unchanged
- Confusing value copy with reference copy
- Assuming separate arrays after assignment
int[] x = {4, 5, 6};
int[] y = new int[3];
y = x;
y[1] = 20;
Console.WriteLine(x[1]);What is the output and why?
Solution
Step 1: Analyze array assignment
Initially,ypoints to a new array, but theny = x;makesyreference the same array asx.Step 2: Effect of modifying
Changingy[1]y[1]to 20 modifies the shared array, sox[1]also becomes 20.Final Answer:
20, becauseyreferences the same array asxafter assignment. -> Option CQuick Check:
Reference assignment means both variables share array [OK]
- Thinking
yremains a separate array after assignment - Assuming array elements are copied automatically
- Confusing initialization with assignment
original so that modifying the copy does NOT affect the original. Which code correctly achieves this in C#?Solution
Step 1: Understand reference vs copy
Assigningcopy = original;copies the reference, so changes affect both.Step 2: Using
Clone()and LINQSelectClone()creates a shallow copy of the array, andSelectwithToArray()creates a new array with copied elements.Step 3: Confirm both methods create independent arrays
BothClone()andSelect(...).ToArray()produce new arrays, so modifying the copy won't affect the original.Final Answer:
Both B and D -> Option AQuick Check:
UseClone()or LINQ to copy arrays [OK]
Clone() or LINQ to copy arrays, not assignment [OK]- Using assignment instead of copying
- Assuming
Clone()returns reference - Not realizing LINQ creates new array
Ready to prove it?
Pass the test and this pattern moves unknown
