Convert a collection to an array - CSharp System

CSharp examples for System:Array Convert

Description

Convert a collection to an array

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from   www .  j  a  v a2 s. com*/

public class Main{
        /// <summary>
        /// Convert a collection to an array
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static T[] ToArray<T>(ICollection<T> collection)
        {
            T[] result = new T[collection.Count];
            int index = 0;
            foreach (T item in collection)
                result[index++] = item;
            return result;
        }
}

Related Tutorials