Open
Description
Nesting a collection initializer inside an object initializer can have unexpected side effects or lead to crashes:
var x = new Evil { Arguments = { "two", "three" } };
Console.WriteLine(x.Arguments.Count); // 3!
class Evil
{
public List<string> Arguments { get; } = [ "one" ];
}
var x = new Evil { Arguments = { "two", "three" } }; // NRE!
class Evil
{
public List<string> Arguments { get; }
}
Even if the argument is initialized with an empty collection (and the constructor does not do any shenanigans either), this kind of code relies heavily on implementation details and is unintuitive to understand.