Convert ICollection to T[] : ICollection « Collections Data Structure « C# / C Sharp






Convert ICollection to T[]

      
using System;
using System.Collections.Generic;
using System.Collections;


  public static class CollectionUtils
  {

    public static T[] ToArray<T>(this ICollection collection)
    {
      T[] res = new T[collection.Count];
      int i = 0;
      foreach (T elem in collection)
      {
        res[i] = elem;
        i++;
      }
      return res;
    }

  }

   
    
    
    
    
    
  








Related examples in the same category

1.Finds a value of the given type in the given collection.
2.Adds a new element to the specified collection.
3.Adds all of the elements of the "c" collection to the "target" collection.
4.Removes all the elements from the collection.
5.Determines whether the collection contains the specified element.
6.Removes the specified element from the collection.
7.Retains the elements in the target collection that are contained in the specified collection
8.Returns an array containing all the elements of the collection.
9.Converts an ICollection instance to an ArrayList instance.
10.Tests if the specified object is a collection and converts it to its string representation.
11.Determines whether the collection contains the specified element
12.Adds the specified element to the specified collection
13.Determines whether the collection contains all the elements in the specified collection.
14.Removes all the elements from the target collection that are contained in the source collection.
15.Converts an System.Collections.ICollection instance to an System.Collections.ArrayList instance.
16.Copies the elements of the ICollection to a new array of the specified element type.
17.Determine whether a given collection only contains a single unique object
18.Is a Collection Null Or Empty Or Default
19.Converts the specified collection to its string representation.
20.Group the collection using a function which returns the key.
21.Convert ICollection to T[]
22.ConvertAll ICollection to TOut[] with Converter
23.Add range to Collection
24.Lambda Collections Generic Set