Returns an array containing all the elements of the collection.
#region License and Copyright
/*
* Dotnet Commons Collections
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
*/
#endregion
using System;
using System.Collections;
namespace Dotnet.Commons.Lang {
class MainClass{
/// <summary>
/// Returns an array containing all the elements of the collection.
/// </summary>
/// <returns>The array containing all the elements of the collection.</returns>
public static System.Object[] ToArray(System.Collections.ICollection c)
{
int index = 0;
System.Object[] objects = new System.Object[c.Count];
System.Collections.IEnumerator e = c.GetEnumerator();
while (e.MoveNext())
objects[index++] = e.Current;
return objects;
}
/// <summary>
/// Obtains an array containing all the elements of the collection.
/// </summary>
/// <param name="objects">The array into which the elements of the collection will be stored.</param>
/// <returns>The array containing all the elements of the collection.</returns>
public static System.Object[] ToArray(System.Collections.ICollection c, System.Object[] objects)
{
int index = 0;
System.Type type = objects.GetType().GetElementType();
System.Object[] objs = (System.Object[])Array.CreateInstance(type, c.Count);
System.Collections.IEnumerator e = c.GetEnumerator();
while (e.MoveNext())
objs[index++] = e.Current;
//If objects is smaller than c then do not return the new array in the parameter
if (objects.Length >= c.Count)
objs.CopyTo(objects, 0);
return objs;
}
}
}
Related examples in the same category