Complete the code to create a new list that does not share the reference with the original list.
List<int> original = new List<int> {1, 2, 3};
List<int> copy = original[1];
copy.Add(4);
Console.WriteLine(original.Count);Using .ToList() creates a new list copy, so changes to copy don't affect original.
Complete the code to avoid modifying the original array when changing the copy.
int[] original = {1, 2, 3};
int[] copy = (int[])original[1];
copy[0] = 10;
Console.WriteLine(original[0]);Clone() creates a shallow copy of the array, so modifying copy does not affect original.
Fix the error in the code to prevent shared reference issues with objects.
class Person { public string Name; } Person p1 = new Person { Name = "Alice" }; Person p2 = p1; p2.Name = "Bob"; Console.WriteLine([1].Name);
Both p1 and p2 reference the same object, so changing p2.Name also changes p1.Name.
Fill both blanks to create a deep copy of a list of objects to avoid shared references.
List<Person> original = new List<Person> { new Person { Name = "Anna" } };
List<Person> copy = original.Select(p => new Person [1]).ToList();
copy[0].Name = [2];
Console.WriteLine(original[0].Name);Using Select with a new Person copies each object, avoiding shared references. Changing copy[0].Name does not affect original[0].Name.
Fill all three blanks to correctly clone a dictionary and modify the copy without affecting the original.
Dictionary<string, int> original = new Dictionary<string, int> { {"a", 1}, {"b", 2} };
Dictionary<string, int> copy = original.ToDictionary([1], [2]);
copy["a"] = [3];
Console.WriteLine(original["a"]);ToDictionary creates a new dictionary by copying keys and values. Changing copy["a"] does not affect original["a"].