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

Common bugs from reference sharing in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is reference sharing in C#?
Reference sharing happens when two or more variables point to the same object in memory, so changes through one variable affect the others.
Click to reveal answer
beginner
Why can reference sharing cause bugs?
Because modifying an object through one reference changes it for all references, leading to unexpected side effects if not intended.
Click to reveal answer
intermediate
What is a common bug caused by modifying a shared list in C#?
If multiple variables share the same list reference, adding or removing items from one variable changes the list for all, which can cause logic errors.
Click to reveal answer
intermediate
How can you avoid bugs from reference sharing when working with objects?
Create a copy of the object instead of sharing the reference. For example, use cloning or copy constructors to get a new independent object.
Click to reveal answer
advanced
What is a shallow copy vs a deep copy in C#?
A shallow copy duplicates the top-level object but keeps references to nested objects. A deep copy duplicates everything, creating fully independent objects.
Click to reveal answer
What happens if you modify an object through one reference when multiple references share it?
AThe object is duplicated automatically
BOnly the modified reference changes
CAll references see the change
DThe program crashes
Which of these can help avoid bugs from reference sharing?
ACreating copies of objects
BUsing global variables
CSharing references everywhere
DIgnoring object state
What is a shallow copy in C#?
ACopying only primitive types
BCopying everything including nested objects
CCopying references without any duplication
DCopying only the top-level object, sharing nested objects
If two variables share a list reference, what happens when you add an item via one variable?
AThe program throws an error
BThe item appears in both variables' lists
CThe list is duplicated automatically
DOnly the first variable's list changes
Which method is NOT a way to create a copy of an object in C#?
AAssigning one variable to another
BUsing a copy constructor
CUsing MemberwiseClone()
DImplementing ICloneable interface
Explain what reference sharing is and why it can cause bugs in C# programs.
Think about how variables can point to the same object.
You got /4 concepts.
    Describe how to avoid bugs caused by reference sharing when working with objects in C#.
    Consider how to make independent objects.
    You got /4 concepts.