Array as reference type behavior in C Sharp (C#) - Time & Space Complexity
When we work with arrays in C#, it's important to know how operations on them behave over time.
We want to see how the time to run code changes when arrays are passed or modified.
Analyze the time complexity of the following code snippet.
int[] numbers = {1, 2, 3, 4, 5};
int[] copy = numbers; // copy points to the same array
copy[0] = 10; // changes original array
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
This code shows that arrays are reference types, so copying the variable does not copy the data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints each element of the array.
- How many times: It runs once for each element in the array (n times).
The loop runs once for every item in the array, so if the array gets bigger, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The time grows directly with the number of elements.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line with the array size.
[X] Wrong: "Copying an array variable duplicates all its elements immediately."
[OK] Correct: In C#, the variable holds a reference, so copying it just points to the same array, not a new one.
Understanding how arrays behave as references helps you explain how data changes and performance relate in real code.
"What if we created a new array and copied elements one by one? How would the time complexity change?"
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
