예제 #1
0
 /// <summary>
 /// 将数组从指定的索引位置开始使用指定的函数的返回值填充指定的长度。
 /// </summary>
 /// <param name="array">要填充的数组。</param>
 /// <param name="value">返回要填充数组的值的函数。</param>
 /// <param name="startIndex">要开始填充的起始索引。</param>
 /// <param name="length">要填充的数组元素个数。</param>
 /// <returns>填充完毕的数组。</returns>
 /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="System.ArgumentNullException"><paramref name="value"/> 为 <c>null</c>。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/>
 /// 加 <paramref name="length"/> 之和大于数组的长度。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/>
 /// 或 <paramref name="length"/> 小于零。</exception>
 public static Array Fill(this Array array, Func <object> value, int startIndex, int length)
 {
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     if (value == null)
     {
         throw CommonExceptions.ArgumentNull("value");
     }
     if (startIndex < 0)
     {
         throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex);
     }
     if (length < 0 || startIndex + length > array.Length)
     {
         throw CommonExceptions.ArgumentOutOfRange("length", length);
     }
     Contract.Ensures(Contract.Result <Array>() != null);
     return(FillInternal(array, value, startIndex, length));
 }
예제 #2
0
 /// <summary>
 /// 从此实例检索子字符串。子字符串从指定的字符位置开始到字符串的结尾。
 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>,那么表示从字符串结束位置向前计算的位置。
 /// </summary>
 /// <param name="str">要检索子字符串的字符串实例。</param>
 /// <param name="startIndex">此示例中子字符串的起始字符位置(从零开始)。</param>
 /// <returns>与此实例中在 <paramref name="startIndex"/> 处开头到字符串结尾的子字符串等效的一个字符串,
 /// 如果 <paramref name="startIndex"/> 等于此实例的长度或大于等于字符串的长度,则为空字符串("")。</returns>
 /// <exception cref="ArgumentNullException"><paramref name="str"/> 为 <c>null</c>。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 指示的位置不在此实例中。</exception>
 public static string Slice(this string str, int startIndex)
 {
     if (str == null)
     {
         throw CommonExceptions.ArgumentNull("str");
     }
     if (startIndex < -str.Length)
     {
         throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex);
     }
     Contract.Ensures(Contract.Result <string>() != null);
     if (startIndex < 0)
     {
         startIndex += str.Length;
     }
     if (startIndex < str.Length)
     {
         return(str.Substring(startIndex));
     }
     return(string.Empty);
 }
예제 #3
0
        /// <summary>
        /// 从当前数组的右端截取一部分。
        /// </summary>
        /// <typeparam name="T">数组中元素的类型。</typeparam>
        /// <param name="array">从该数组返回其最右端截取的部分。</param>
        /// <param name="length">要截取的元素个数。
        /// 如果为 <c>0</c>,则返回空数组。如果大于或等于 <paramref name="array"/> 的长度,
        /// 则返回整个数组的一个浅拷贝。</param>
        /// <returns>从指定数组的右端截取的部分。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception>
        public static T[] Right <T>(this T[] array, int length)
        {
            CommonExceptions.CheckArgumentNull(array, nameof(array));
            if (length < 0)
            {
                throw CommonExceptions.ArgumentOutOfRange(nameof(length), length);
            }
            Contract.Ensures(Contract.Result <T[]>() != null);
            if (length == 0)
            {
                return(Empty <T>());
            }
            if (length > array.Length)
            {
                length = array.Length;
            }
            var result = new T[length];

            Array.Copy(array, array.Length - length, result, 0, length);
            return(result);
        }
예제 #4
0
        /// <summary>
        /// 从当前数组的指定索引开始截取一部分。
        /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>,那么表示从数组末尾向前计算的位置。
        /// </summary>
        /// <typeparam name="T">数组中元素的类型。</typeparam>
        /// <param name="array">要截取的数组。</param>
        /// <param name="startIndex">要截取的起始索引。</param>
        /// <returns>截取得到的数组。如果 <paramref name="startIndex"/>
        /// 等于数组的长度,则为空数组。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 指示的位置不在此数组中。</exception>
        /// <overloads>
        /// <summary>
        /// 从当前数组的指定索引开始截取一部分。
        /// </summary>
        /// </overloads>
        public static T[] Slice <T>(this T[] array, int startIndex)
        {
            CommonExceptions.CheckArgumentNull(array, nameof(array));
            if (startIndex < -array.Length || startIndex > array.Length)
            {
                throw CommonExceptions.ArgumentOutOfRange(nameof(startIndex), startIndex);
            }
            Contract.Ensures(Contract.Result <T[]>() != null);
            if (startIndex == array.Length)
            {
                return(Empty <T>());
            }
            if (startIndex < 0)
            {
                startIndex += array.Length;
            }
            var result = new T[array.Length - startIndex];

            Array.Copy(array, startIndex, result, 0, result.Length);
            return(result);
        }
예제 #5
0
 /// <summary>
 /// 从当前数组的左端截取一部分。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">从该数组返回其最左端截取的部分。</param>
 /// <param name="length">要截取的元素个数。
 /// 如果为 <c>0</c>,则返回空数组。如果大于或等于 <paramref name="array"/> 的长度,
 /// 则返回整个数组的一个浅拷贝。</param>
 /// <returns>从指定数组的左端截取的部分。</returns>
 /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception>
 public static T[] Left <T>(this T[] array, int length)
 {
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     if (length < 0)
     {
         throw CommonExceptions.ArgumentOutOfRange("length", length);
     }
     Contract.Ensures(Contract.Result <T[]>() != null);
     if (length == 0)
     {
         return(Empty <T>());
     }
     if (length > array.Length)
     {
         length = array.Length;
     }
     T[] result = new T[length];
     Array.Copy(array, result, length);
     return(result);
 }
예제 #6
0
 /// <summary>
 /// 从当前数组的指定索引开始截取指定长度的一部分。
 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>,
 /// 那么表示从字符串结束位置向前计算的位置。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">要截取的数组。</param>
 /// <param name="startIndex">要截取的起始索引。</param>
 /// <param name="length">要截取的数组元素个数。</param>
 /// <returns>截取得到的数组。</returns>
 /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 小于负的此数组的长度。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 加 <paramref name="length"/>
 /// 之和指示的位置不在此数组中。</exception>
 public static T[] Subarray <T>(this T[] array, int startIndex, int length)
 {
     CommonExceptions.CheckArgumentNull(array, "array");
     if (startIndex < -array.Length)
     {
         throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex);
     }
     if (length < 0 || startIndex + length > array.Length)
     {
         throw CommonExceptions.InvalidOffsetLength();
     }
     Contract.Ensures(Contract.Result <T[]>() != null);
     if (length == 0)
     {
         return(Empty <T>());
     }
     if (startIndex < 0)
     {
         startIndex += array.Length;
     }
     T[] result = new T[length];
     Array.Copy(array, startIndex, result, 0, length);
     return(result);
 }