Do collection expressions like [] always create a new object?

4 weeks ago 31
ARTICLE AD BOX

When I have a method like:

int[] GetCollection() => [];

Will every call to it create a new array object or is it optimized, like reusing the object or using Something like Array.Empty<int>()?

And what in the case where I have fixed values like that:

int[] GetCollection() => [100, 200];

I understand that in case I would return a mutable collection like the next example, then none of these optimizations would be possible:

List<int> GetCollection() => [];
Read Entire Article