Re: Deep thoughts on .ToArray()
- From: RayLopez99 <raylopez88@xxxxxxxxx>
- Date: Wed, 7 Jan 2009 18:55:34 -0800 (PST)
On Jan 6, 9:59 pm, Göran Andersson <gu...@xxxxxxxxx> wrote:
RayLopez99 wrote:
Now that I've learned about 90% of whatever there is to learn about C#
in about six months time,
Give it another six months, and you will realise that then you have
really learned about 50%. ;)
You said that six months ago Goran! But I feel like a guru. ;-)
Using ToArray on an array is only possible when targeting framework 3.5
where the IEnumerable interface (which the Array class implements) has
that extension method.
Yes, I have that framework.
The ToArray method creates a Buffer object from the IEnumerable (the
array in this case). This normally means that the items in the
collections are enumerated and added to an internal array one at a time,
expanding the target array when needed. Then the buffer would create yet
another array and copy the items to it and return as result. However, in
the special case when the IEnumerable object also implements ICollection
(which the Array class does), it can use the CopyTo method to copy the
data to the new array without having to enumerate all the items. So, the
items will only be copied once in this case.
Still, there is a bit of overhead when calling the ToArray method
compared with calling the Array.CopyTo method yourself.
OK, I might keep that in mind and use .CopyTo in the future
Also, it's not
so obvious that the ToArray method actually creates a shallow copy. For
an array the ToArray method could just have returned the array object
itself, just as the ToString method of the String class just returns the
string object itself.
OK, I understand you mean "obvious" as in somebody else reading your
code, who is not experienced in C#.
Thanks for your help.
RL
.
- Follow-Ups:
- Re: Deep thoughts on .ToArray()
- From: Göran Andersson
- Re: Deep thoughts on .ToArray()
- References:
- Deep thoughts on .ToArray()
- From: RayLopez99
- Re: Deep thoughts on .ToArray()
- From: Göran Andersson
- Deep thoughts on .ToArray()
- Prev by Date: Discovery: dictionaries load slow unless you have the right key/value pair in the right format
- Next by Date: Re: C# forms
- Previous by thread: Re: Deep thoughts on .ToArray()
- Next by thread: Re: Deep thoughts on .ToArray()
- Index(es):
Relevant Pages
|