Do not use default constructor of ImmutableArray<T> #34096
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Collections
code-analyzer
Marks an issue that suggests a Roslyn analyzer
code-fixer
Marks an issue that suggests a Roslyn code fixer
Milestone
All structs have a default constructor, so we can't prevent people from writing code like this:
The reason this doesn't work is
ImmutableArray<T>
is just wrapping an array. By calling the default constructor the underlying field storing the array is initialized tonull
. For performance reason it's not viable to havenull
-checks in all methods that access this field.Unfortunately we can't obsolete the default struct constructor because C# doesn't let us define one.
Proposal
All expressions of the form
new ImmutableArray<T>()
should be flagged. A code fixer should offer to replace those withImmutableArray<T>.Empty
.The text was updated successfully, but these errors were encountered: