Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

ToArray Operator

Modified: 2007/01/24 04:52 by 71.35.131.116 - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The ToArray operator creates an array from a sequence. This allows you to force an immediate enumerations over an interator.

Edit

Method Signatures

public static TSource[] ToArray<TSource>(
    this IEnumerable<TSource> source)



Edit

Exceptions

Throws an ArgumentNullException if source is null.


Edit

Pseudo-code

If source is null, throw an ArgumentNullException.
If source is an ICollection type.
If source has no elements, return an empty array (TSource0).
Create a new array the same length as source.Count.
Call ICollection.CopyTo which will copy data from the source sequence to a new array.
Return the new array.
Else.
Initialize a new array with a length of 4.
Iterate the source sequence.
If there isn't enough room in the new array to copy the next element, double the size of the new array.
Copy the current element to the new array.
Return the new array.

Edit

Loop Count

1. All elements in source are enumerated.


Edit

Code Samples

TODO: Needs code sample.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.