Generic Array Helper Class
using System; public static class ArrayHelper { /// <summary> /// Clear T type single dimensional array to either the default value or a set value. (ArrayHelper.cs) /// </summary> public static void Clear<T>(this T[] array, T value = default) { if (array == null) throw new Exception("The array cannot be null."); for (var i = 0; i < array.Length; i++) array[i] = value; } /// <summary> /// Clear T type multi dimensional array to either the default value or a set value. (ArrayHelper.cs) /// </summary> public static void Clear<T>(this T[][] array, T value = default) { if (array == null) throw new Exception("The array cannot be null."); var xlen = array.GetLength(0); var ylen = array.GetLength(1); for (var x = 0; x < xlen; x++) for (var y = 0; y < ylen; y++) array[x][y] = value; } /// <summary> /// Copies a total or portion of a source array of type T starting at offset /// and copying count bytes. (ArrayHelper.cs) /// </summary> public static T[] SubArray<T>(this T[] array, int offset, int count) { if (array == null) throw new Exception("The array cannot be null."); if (count - offset > array.Length) throw new Exception($"Count {count} + Offset {offset} must be less than the length of the array {array.Length}"); var result = new T[count]; Array.Copy(array, offset, result, 0, count); return result; } /// <summary> /// Copies the source array of type T into the target array. /// (ArrayHelper.cs) /// </summary> public static void Copy<T>(this T[] array, T[] array1) { if (array == null || array1 == null) throw new Exception("Neither the source or target array can be null."); if (array == null || array1 == null) throw new Exception("Neither the source or target array can be null."); var len = array.Length; var len1 = array1.Length; if (len > len1) throw new Exception("The target array must be the same length."); Array.Copy(array, 0, array1, 0, len); } /// <summary> /// Inverts the order of the array. /// (ArrayHelper.cs) /// </summary> public static T[] Invert<T>(this T[] array) { if (array == null) throw new Exception("The array cannot be null."); var len = array.Length; var t = new T[len]; for (int i = len - 1, j = 0; i >= 0; t[j++] = array[i--]) ; return t; } public static T[] ReSize<T>(this T[] array, int NewSize) { if (array == null) throw new Exception("The array cannot be null."); if (array.Length >= NewSize) return array; var temp = new T[NewSize]; Array.Copy(array, 0, temp, 0, array.Length); return temp; } public static T[] ReSize<T>(this T[] array) { if (array == null) throw new Exception("The array cannot be null."); var cSize = (long) array.Length; var mul = 1.2; if (cSize < int.MaxValue >> 1) mul = 8; var nSize = (long) (cSize * mul); if (nSize > int.MaxValue) nSize = int.MaxValue; var temp = new T[nSize]; Array.Copy(array, 0, temp, 0, array.Length); return temp; } public static T[][] ReSize<T>(this T[][] array, int NewX, int NewY) { if (array == null) throw new Exception("The array cannot be null."); var xlen = array.GetLength(0); var ylen = array.GetLength(1); if (xlen >= NewX && ylen >= NewY) return array; var temp = new T[NewX][]; for (var i = 0; i < NewX; ++i) temp[i] = new T[NewY]; for (var i = 0; i < NewX; ++i) Array.Copy(array[i], temp[i], ylen); return temp; } }