Re: Deep thoughts on .ToArray()

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Deep thoughts on .ToArray()
    ... You are creating an array that is just thrown away when the reference is replaced with another array in the next line. ... 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. ... there is a bit of overhead when calling the ToArray method compared with calling the Array.CopyTo method yourself. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: setSize ArrayList, when will it come?
    ... Call the other collection's toArray method. ... It would be nice if ArrayList used a loop to do the copy for small added collections; it could cut over to the array method for larger addends. ... So just think, if you unionize, Marlon Brando might play ... YOU in a movie. ...
    (comp.lang.java.programmer)
  • Re: Vector#toArray()
    ... > Allan Bruce wrote: ... >>> Vector's toArray method is synchronized, ... > Accesses to the array will be synchronized if, and only if, ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: int [] Array Question
    ... int[] as a param. ... In that case you should use the ToArray method of either ArrayList or ... You can't add values to an array - arrays are fixed size. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generics in .NET
    ... "Rudy Velthuis" wrote in message ... I will be able to test this tomorrow when I get to a PC with Rad Studio on it, but I assume that a list of type String's ToArray method will create a Array of String rather than an Array of Object. ...
    (alt.comp.lang.borland-delphi)