Repository: dotnet/efcore
File: src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs (C#)

Code before:

            foreach (var propertyValues in propertyValuesList)
            {
                list.Add(propertyValues?.ToObject() ?? complexProperty.ComplexType.ClrType.GetDefaultValue());
            }
        }

        return structuralObject;
    }

    private object SetNestedComplexPropertyValue(object structuralObject, IComplexProperty complexProperty, object? value)
    {
        return UseOldBehavior37516
            ? ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValue(structuralObject, value)
            : SetValueRecursively(structuralObject, complexProperty.GetChainToComplexProperty(fromEntity: false), 0, value);

        static object SetValueRecursively(object instance, IReadOnlyList<IComplexProperty> chain, int index, object? value)
        {
            var currentProperty = (IRuntimeComplexProperty)chain[index];
            if (index == chain.Count - 1)
            {
                return currentProperty.GetSetter().SetClrValue(instance, value);
            }

            var child = currentProperty.GetGetter().GetClrValue(instance);
            if (child == null)
            {
                return instance;
            }

            var updated = SetValueRecursively(child, chain, index + 1, value);
            // Need to update the child value as well because it could be a value type
            // TODO: Improve this, see #36041
            return currentProperty.GetSetter().SetClrValue(instance, updated);
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override bool IsNullableComplexPropertyNull(int index)
        => _nullComplexPropertyFlags != null && _nullComplexPropertyFlags[index];

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.

Review comment (suggestion):
Especially because this is an arbitrary object where the equality operator might be overridden.

```suggestion
            if (child is null)
```

Code after:

            foreach (var propertyValues in propertyValuesList)
            {
                list.Add(propertyValues?.ToObject() ?? complexProperty.ComplexType.ClrType.GetDefaultValue());
            }
        }

        return structuralObject;
    }

    private object SetNestedComplexPropertyValue(object structuralObject, IComplexProperty complexProperty, object? value)
    {
        return UseOldBehavior37516
            ? ((IRuntimeComplexProperty)complexProperty).GetSetter().SetClrValue(structuralObject, value)
            : SetValueRecursively(structuralObject, complexProperty.GetChainToComplexProperty(fromEntity: false), 0, value);

        static object SetValueRecursively(object instance, IReadOnlyList<IComplexProperty> chain, int index, object? value)
        {
            var currentProperty = (IRuntimeComplexProperty)chain[index];
            if (index == chain.Count - 1)
            {
                return currentProperty.GetSetter().SetClrValue(instance, value);
            }

            var child = currentProperty.GetGetter().GetClrValue(instance);
            if (child is null)
            {
                return instance;
            }

            var updated = SetValueRecursively(child, chain, index + 1, value);
            // Need to update the child value as well because it could be a value type
            // TODO: Improve this, see #36041
            return currentProperty.GetSetter().SetClrValue(instance, updated);
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override bool IsNullableComplexPropertyNull(int index)
        => _nullComplexPropertyFlags != null && _nullComplexPropertyFlags[index];

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.