public static void ThrowIf_Null_InvalidLength <T>(int minLength, int maxLength, params IEnumerable <T>[] collections) { ThrowUtils.ThrowIf_Negative(minLength, maxLength); ThrowUtils.ThrowIf_IntervalInvalid(minLength, maxLength, true); if (collections == null) { return; } for (int i = 0; i < collections.Length; i++) { IEnumerable <T> c = collections[i]; if (c == null) { throw new ArgumentNullException($"Argument {i + 1} of {collections.Length} cannot have NULL value"); } else { int count = c.Count(); if (count > maxLength || count < minLength) { throw new ArgumentException($"Argument {i + 1} of {collections.Length} have invalid length: {count}"); } } } }
public static List <T> CreateList <T>(T element, int count) { ThrowUtils.ThrowIf_Negative(count); List <T> collection = new List <T>(); for (int i = 0; i < count; i++) { collection.Add(element); } return(collection); }
public static List <int> CreateListByRange(int from, int step, int count) { ThrowUtils.ThrowIf_Negative(count); List <int> collection = new List <int>(); for (int i = 0; i < count; i++) { collection.Add(from + step * i); } return(collection); }
//public static T CreateJaggedArray<T>(params int[] lengths) //{ // var type = typeof(T); // var elementType = getRootElementType(); // var depth = getDepth(); // var reverseLengths = lengths.Reverse().ToArray(); // Array array = Array.CreateInstance(elementType, reverseLengths[0]); // for (int i = 1; i < depth; i++) // { // var length = reverseLengths[i]; // var tmpArray = Array.CreateInstance(array.GetType(), length); // for (int k = 0; k < length; k++) // { // var tmp = // Array.Copy(array, tmpArray, 1); // length.SetValue(Array.Copy(array, tmpArray)) // } // } // Type getRootElementType() // { // var t = type; // while (t.GetElementType().IsArray) // { // t = t.GetElementType(); // } // return t.GetElementType(); // } // int getDepth() // { // var i = 1; // var t = type; // while (t.GetElementType().IsArray) // { // t = t.GetElementType(); // i++; // } // return i; // } //} #endregion #region ##### List ##### public static List <T> CreateList <T>(int count, T initial, Func <T, T> factory) { ThrowUtils.ThrowIf_Negative(count); List <T> collection = new List <T>(); var last = initial; for (int i = 0; i < count; i++) { var current = factory(last); collection.Add(current); last = current; } return(collection); }