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

Array as reference type behavior in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that arrays are reference types in C#?
Arrays store a reference (or address) to the actual data in memory, not the data itself. When you assign one array variable to another, both variables point to the same array in memory.
Click to reveal answer
beginner
What happens if you change an element of an array through one variable when arrays are reference types?
The change is visible through all variables that reference the same array because they all point to the same data in memory.
Click to reveal answer
intermediate
How can you create a copy of an array to avoid reference sharing?
You can create a new array and copy elements manually or use methods like Array.Copy or the Clone method to create a separate copy in memory.
Click to reveal answer
beginner
Consider this code:<br>
int[] a = {1, 2, 3};<br>int[] b = a;<br>b[0] = 10;
<br>What is the value of a[0] after this code runs?
The value of a[0] is 10 because both a and b reference the same array. Changing b[0] changes the array itself.
Click to reveal answer
intermediate
Why is understanding array reference behavior important in programming?
It helps avoid bugs caused by unintended changes to shared data and allows you to manage memory and data flow correctly.
Click to reveal answer
In C#, what happens when you assign one array variable to another?
AThe array elements are copied one by one.
BA new copy of the array is created automatically.
CThe array is converted to a list.
DBoth variables point to the same array in memory.
If you modify an element of an array through one variable, what happens to other variables referencing the same array?
AThey throw an error.
BThey keep the old value.
CThey see the updated value.
DThey create a new array automatically.
Which method can you use to create a separate copy of an array in C#?
AArray.Copy
BArray.ReferenceEquals
CArray.Clear
DArray.Resize
What is the type of an array in C#?
AReference type
BValue type
CPrimitive type
DInterface type
Why might unintended sharing of arrays cause bugs?
ABecause arrays cannot be modified.
BBecause changes through one variable affect all references.
CBecause arrays are immutable.
DBecause arrays are copied automatically.
Explain how arrays behave as reference types in C# and what happens when you assign one array variable to another.
Think about how memory addresses work for arrays.
You got /4 concepts.
    Describe how to create a separate copy of an array to avoid shared reference issues.
    Consider methods that duplicate array data.
    You got /4 concepts.