CollectionHelpers
using System.Collections.Generic;
namespace System.Collections.ObjectModel
{
internal static class CollectionHelpers
{
internal static void ValidateCopyToArguments(int sourceCount, Array array, int index)
{
if (array == null)
throw new ArgumentNullException("array");
if (array.Rank != 1)
throw new ArgumentException(System.SR.Arg_RankMultiDimNotSupported, "array");
if (array.GetLowerBound(0) != 0)
throw new ArgumentException(System.SR.Arg_NonZeroLowerBound, "array");
if (index < 0 || index > array.Length)
throw new ArgumentOutOfRangeException("index");
if (array.Length - index < sourceCount)
throw new ArgumentException(System.SR.Arg_ArrayPlusOffTooSmall);
}
internal static void CopyTo<T>(ICollection<T> collection, Array array, int index)
{
ValidateCopyToArguments(collection.Count, array, index);
ICollection collection2 = collection as ICollection;
if (collection2 != null)
collection2.CopyTo(array, index);
else {
T[] array2 = array as T[];
if (array2 != null)
collection.CopyTo(array2, index);
else {
object[] array3 = array as object[];
if (array3 == null)
throw new ArgumentException(System.SR.Argument_IncompatibleArrayType, "array");
try {
foreach (T item in (IEnumerable<T>)collection) {
array3[index++] = item;
}
} catch (ArrayTypeMismatchException) {
throw new ArgumentException(System.SR.Argument_IncompatibleArrayType, "array");
}
}
}
}
}
}