-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Conversation
return Array.Empty<T>(); | ||
} | ||
#endif | ||
|
||
T[] array = new T[_size]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider adding a method like the following:
T[] Allocate<T>(int size)
{
#if FEATURE_CORCLR
if (size == 0) { return Array.Empty<T>(); }
#endif
return new T[size];
}
This could then become a drop in replacement for new T[size]
in places where Array.Empty<T>
was acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea. Though in a case like this (and I expect others) you'd end up doing something like:
T[] array = Allocate(_size);
Array.Copy(_items, 0, array, 0, _size);
return array;
and you'd be doing a bit of extra work unnecessarily with the Array.Copy call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree there would be some extra work here in copy. Feel like more Array.Empty<T>
overall would get more use if we have a clean, simple way to have allocations return it automatically.
ae8f99f
to
5bcecb9
Compare
Is dotnet/corefx#5562 going to be updated to return |
Yes, I just haven't had a chance yet. |
LGTM |
Use _emptyArray in List<T>.ToArray
In dotnet/corefx#2363, @terrajobst says:
I see it is within |
This isn't the code for the full framework. I expect when it's ported back, a switch will be used. |
Ah, I was under the impression the code was shared -- didn't realize a port step would be necessary. Thanks! |
Unfortunately. 😦 At least with the current state of things. It could change. |
I've only added this to
List<T>
in mscorlib as it's the only one of the collections with a ToArray that's exposed out (I've not touched legacy collections like ArrayList). The other changes for the other collections are in corefx in dotnet/corefx#5562.cc: @jkotas, @ellismg
https://github.com/dotnet/corefx/issues/2363