ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Immutable Record Deep Copy with Recursion

C#

Goal -- WPM

Ready
Exercise Algorithm Area
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5public record Address(string Street, string City, string ZipCode);
6public record Person(
7int Id,
8string Name,
9Address HomeAddress,
10List<Address> WorkAddresses // Mutable List, but Address records are immutable
11);
12
13public static class ImmutableCopier
14{
15public static Address DeepCopyAddress(Address address)
16{
17if (address == null)
18{
19return null;
20}
21// Address records are immutable, so a direct copy is sufficient
22return new Address(address.Street, address.City, address.ZipCode);
23}
24
25public static Person DeepCopyPerson(Person person)
26{
27if (person == null)
28{
29return null;
30}
31
32// Deep copy the nested Address record
33Address copiedHomeAddress = DeepCopyAddress(person.HomeAddress);
34
35// Deep copy the List of Addresses
36List<Address> copiedWorkAddresses = null;
37if (person.WorkAddresses != null)
38{
39copiedWorkAddresses = new List<Address>(person.WorkAddresses.Count);
40foreach (var addr in person.WorkAddresses)
41{
42copiedWorkAddresses.Add(DeepCopyAddress(addr));
43}
44}
45
46// Create a new Person record with copied nested objects
47return new Person(
48Id: person.Id,
49Name: person.Name,
50HomeAddress: copiedHomeAddress,
51WorkAddresses: copiedWorkAddresses
52);
53}
54
55// Example of a more complex scenario requiring recursive copying if Person contained Person
56// For this specific problem, Address is the only nested immutable record.
57}
Algorithm description viewbox

Immutable Record Deep Copy with Recursion

Algorithm description:

This C# code demonstrates how to perform a deep copy of an immutable `Person` record that contains nested immutable `Address` records and a `List<Address>`. The `DeepCopyPerson` method recursively copies the `Address` objects and creates a new `List<Address>`, ensuring that the new `Person` object is entirely independent of the original. This is vital when dealing with complex immutable data structures to prevent unintended side effects and maintain data integrity.

Algorithm explanation:

The `ImmutableCopier` class provides methods for deep copying immutable records. `DeepCopyAddress` handles the base case: since `Address` is an immutable record, creating a new instance with the same property values effectively creates a copy. `DeepCopyPerson` handles the more complex structure. It first checks for null input. It then calls `DeepCopyAddress` to create a new, independent copy of the `HomeAddress`. For `WorkAddresses`, which is a `List<Address>`, it checks if the list is null. If not, it creates a new `List<Address>` and iterates through the original list, calling `DeepCopyAddress` for each `Address` within the list and adding the copied `Address` to the new list. Finally, it constructs and returns a new `Person` record using the copied `HomeAddress` and `WorkAddresses`. This ensures that modifying the copied `Person` or its nested `Address` objects will not affect the original `Person` object or its components. The time complexity is O(N + M), where N is the number of `Address` objects in `WorkAddresses` and M is the number of properties in `Person` and `Address` (effectively constant for these small records), as each component is visited and copied once. Space complexity is also O(N + M) to store the new objects.

Pseudocode:

Record Address(Street, City, ZipCode)
Record Person(Id, Name, HomeAddress, WorkAddresses (List<Address>))

Function DeepCopyAddress(address):
  If address is null, return null.
  Return new Address(address.Street, address.City, address.ZipCode).

Function DeepCopyPerson(person):
  If person is null, return null.
  Copy HomeAddress: copiedHomeAddress = DeepCopyAddress(person.HomeAddress).
  Initialize copiedWorkAddresses as an empty List<Address>.
  If person.WorkAddresses is not null:
    For each addr in person.WorkAddresses:
      Add DeepCopyAddress(addr) to copiedWorkAddresses.
  Return new Person(person.Id, person.Name, copiedHomeAddress, copiedWorkAddresses).