0

I just want to know if the implementation of LINQ protects the developer against performance hits when calling ToArray() from an Array.

For example,

var array1 = Enumerable.Range(0,short.MaxValue).ToArray(); //does the actual copy
var array2 = array1.ToArray() //is this the same object as array1?
var array3 = ((IEnumerable<int>)array2).ToArray() //what about this?

Do all three of these ToArray() calls have the same performance implications?

Other questions like this have addressed the general performance implications of ToArray(),

Is it better to call ToList() or ToArray() in LINQ queries?

Linq ToList/ToArray/ToDictionary performance

but I'm curious as to whether the implementation is "smart enough" to avoid copying elements to another array if the source is an array.

I printed all of the hash codes from array 1, 2, and 3.

    Console.WriteLine(array1.GetHashCode());
    Console.WriteLine(array2.GetHashCode());
    Console.WriteLine(array3.GetHashCode());

and got

62476613 11404313 64923656

Since the hash codes are all different does that mean that a copy operation happened all three times?

Community
  • 1
  • 1
C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
  • No Shortcut. See http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,783a052330e7d48d,references – Ralf Jul 02 '15 at 16:35
  • No, it always returns a new copy http://stackoverflow.com/questions/2092811/does-array-toarray-return-the-original-array-if-it-is-the-same-type – keyboardP Jul 02 '15 at 16:35
  • 1
    You've already shown, in the question, that different arrays are returned, so why are you asking the question when you've already gotten your answer? – Servy Jul 02 '15 at 16:36
  • bah... i didn't see that answer. I duplicated the question then.. – C. Tewalt Jul 02 '15 at 16:36
  • @Servy I've been pestered before to show better research effort in questions – C. Tewalt Jul 02 '15 at 16:37
  • @matrixugly And rightly so. And, in doing that research, you appear to have already found your answer. Having already found your answer why did you post the question anyway? – Servy Jul 02 '15 at 16:38
  • @Servy isn't the purpose of SO to share helpful things you find? – C. Tewalt Jul 02 '15 at 16:39
  • This has been answered by multiple people now, so I won't, but another test you could've done is to use `Object.ReferenceEquals()`. That's not to say the items in the arrays aren't the same as the original elements. If the type of the `IEnumerable` is a value type, then they are copies, if the type is a reference type, then they are the same object, just a different pointer. – Brian Ball Jul 02 '15 at 16:40
  • @matrixugly It's to be a repository of useful questions with useful answers. When the information you've found is already readily apparent and accessible, duplicating it is not adding value. – Servy Jul 02 '15 at 16:41
  • @Servy OK. You asked why I posted the question when I had also posted some research on the question. Granted, it was a duplicate- I missed that. – C. Tewalt Jul 02 '15 at 16:43
  • @matrixugly - side note on referencing other posts - consider posting links directly which will auto-magically turn into titles of the post instead of "this question" text. This saves one time to figure out what questions you are linking to. – Alexei Levenkov Jul 02 '15 at 16:53
  • 1
    @AlexeiLevenkov - thanks, that makes sense. I edited this question. – C. Tewalt Jul 02 '15 at 16:58

2 Answers2

2

Does ToArray() from an array return itself?

No it doesn't. It returns a new copy of the array with each element copied to the new array.

Do all three of these ToArray() calls have the same performance implications?

The first one: Enumerable.Range(0,short.MaxValue).ToArray(); creates an array from IEnumerable<T>.

Second one: var array2 = array1.ToArray(), It will create a new array with each element copied to array2, so array1 and array2 will have different references.

Third one: var array3 = ((IEnumerable<int>)array2).ToArray() is just redundant code, since the array is already IEnumerable<int> so casting it to it will not make a difference.

Habib
  • 219,104
  • 29
  • 407
  • 436
1

Does ToArray() from an array return itself?

I will say no, beause of MSDN's own words:

Creates an array from a IEnumerable.

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37