Sometimes, when you share objects between parts of your program, changes in one place can unexpectedly affect others. This happens because they share the same reference, not a copy.
Common bugs from reference sharing in C Sharp (C#)
ClassName obj1 = new ClassName(); ClassName obj2 = obj1; // obj2 references the same object as obj1
Assigning one object variable to another copies the reference, not the actual object.
Both variables point to the same object in memory, so changes via one affect the other.
List<int> list1 = new List<int> {1, 2, 3}; List<int> list2 = list1; list2.Add(4); Console.WriteLine(string.Join(",", list1));
class Person { public string Name; } Person p1 = new Person { Name = "Alice" }; Person p2 = p1; p2.Name = "Bob"; Console.WriteLine(p1.Name);
This program shows that adding an item to fruits2 also adds it to fruits1 because both variables share the same list reference.
using System; using System.Collections.Generic; class Program { static void Main() { List<string> fruits1 = new List<string> { "Apple", "Banana" }; List<string> fruits2 = fruits1; // Both point to the same list fruits2.Add("Cherry"); Console.WriteLine("fruits1 contains:"); foreach (var fruit in fruits1) { Console.WriteLine(fruit); } } }
To avoid this bug, create a copy of the object instead of copying the reference.
For collections, use methods like List<T>.ToList() to clone.
For custom classes, implement a method to clone or copy the object.
Assigning one object variable to another copies the reference, not the object.
Changes through one reference affect all references to the same object.
To prevent bugs, create copies when you want independent objects.
Practice
Solution
Step 1: Understand object assignment in C#
Assigning one object variable to another copies the reference, not the object itself.Step 2: Effect of reference copying
Both variables point to the same object in memory, so changes via one affect the other.Final Answer:
Both variables refer to the same object in memory. -> Option AQuick Check:
Reference copy = same object [OK]
- Thinking a new object is created automatically
- Assuming variables hold independent objects
- Confusing value types with reference types
Solution
Step 1: Identify how to copy objects in C#
Assignment copies references, so to create a copy, you need cloning methods like MemberwiseClone() or ICloneable.Step 2: Why other options are incorrect
Assignment operator copies reference; readonly only prevents reassignment; 'new' without copying fields creates a new empty object.Final Answer:
Use a method like MemberwiseClone() or implement ICloneable. -> Option AQuick Check:
Clone method = copy object [OK]
- Using assignment operator expecting a copy
- Confusing readonly with copying
- Creating new object without copying data
class Box { public int Value; }
var box1 = new Box { Value = 5 };
var box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);Solution
Step 1: Understand reference assignment in the code
box2 is assigned box1, so both refer to the same Box object.Step 2: Effect of changing box2.Value
Changing box2.Value to 10 changes the single shared object, so box1.Value also becomes 10.Final Answer:
10 -> Option DQuick Check:
Shared reference means value changes reflect [OK]
- Expecting box1.Value to remain 5
- Confusing value and reference types
- Assuming box2 is a separate object
class Person { public string Name; }
var p1 = new Person { Name = "Alice" };
var p2 = p1;
p2.Name = "Bob";
Console.WriteLine(p1.Name);Solution
Step 1: Analyze object references in the code
p2 is assigned p1, so both variables point to the same Person object.Step 2: Effect of modifying p2.Name
Changing p2.Name to "Bob" changes the shared object, so p1.Name also becomes "Bob".Final Answer:
p2.Name change affects p1.Name because both reference the same object. -> Option BQuick Check:
Shared reference causes shared changes [OK]
- Assuming p1.Name remains "Alice"
- Expecting automatic deep copy on assignment
- Thinking null reference exception occurs
Solution
Step 1: Understand reference sharing with lists
Assigning list2 = list1 copies the reference, so both variables point to the same list.Step 2: Creating an independent copy
Using new List<int>(list1) creates a new list copying elements, so list2 is independent.Final Answer:
var list2 = new List<int>(list1); list2.Add(4); -> Option CQuick Check:
New list constructor copies elements [OK]
- Assigning list2 = list1 expecting copy
- Clearing list1 expecting list2 unaffected
- Reassigning list1 does not affect list2 reference
Ready to prove it?
Pass the test and this pattern moves unknown
