Gets the array from IEnumerable and ICollection. - CSharp System

CSharp examples for System:Array Enumerate

Description

Gets the array from IEnumerable and ICollection.

Demo Code

// Copyright (c) 2014 Eser Ozvataf (eser@sent.com). All rights reserved.
using System.Collections.Generic;

public class Main{
        /// <summary>
        /// Gets the array.
        /// </summary>
        /// <typeparam name="T">The type array contains</typeparam>
        /// <param name="enumerable">The enumerable</param>
        /// <returns>Array of given type</returns>
        public static T[] GetArray<T>(IEnumerable<T> enumerable)
        {//  w  ww  .  j  a  v  a2 s.c o  m
            List<T> collection = new List<T>(enumerable);
            return collection.ToArray();
        }
        // methods

        /// <summary>
        /// Gets the array.
        /// </summary>
        /// <typeparam name="T">The type array contains</typeparam>
        /// <param name="collection">The collection</param>
        /// <returns>Array of given type</returns>
        public static T[] GetArray<T>(ICollection<T> collection)
        {
            T[] array = new T[collection.Count];
            collection.CopyTo(array, 0);

            return array;
        }
}

Related Tutorials