コード例 #1
0
ファイル: ArrayHelper.cs プロジェクト: qube7/qcomposite
        /// <summary>
        /// Returns a new array in which a specified element is inserted at the specified index in the specified array.
        /// </summary>
        /// <typeparam name="T">The type of the elements of the array.</typeparam>
        /// <param name="array">The one-dimensional, zero-based array in which to insert the element.</param>
        /// <param name="index">The zero-based index at which to insert the element.</param>
        /// <param name="value">The object to insert.</param>
        /// <returns>A new array with <paramref name="value"/> inserted at the <paramref name="index"/> position in the <paramref name="array"/>.</returns>
        public static T[] Insert <T>(T[] array, int index, T value)
        {
            Requires.NotNull(array, nameof(array));

            if (index < 0 || index > array.Length)
            {
                throw Error.ArgumentOutOfRange(Strings.IndexRangeArray, nameof(index));
            }

            if (array.Length > 0)
            {
                T[] buffer = new T[array.Length + 1];

                if (index > 0)
                {
                    Array.Copy(array, 0, buffer, 0, index);
                }

                buffer[index] = value;

                if (index < array.Length)
                {
                    Array.Copy(array, index, buffer, index + 1, array.Length - index);
                }

                return(buffer);
            }

            return(Params.Array(value));
        }
コード例 #2
0
ファイル: ArrayHelper.cs プロジェクト: qube7/qcomposite
        /// <summary>
        /// Returns a new array in which a specified element is inserted at the end of the specified array.
        /// </summary>
        /// <typeparam name="T">The type of the elements of the array.</typeparam>
        /// <param name="array">The one-dimensional, zero-based array in which to insert the element.</param>
        /// <param name="value">The object to insert.</param>
        /// <returns>A new array with <paramref name="value"/> inserted at the end of the <paramref name="array"/>.</returns>
        public static T[] Append <T>(T[] array, T value)
        {
            Requires.NotNull(array, nameof(array));

            if (array.Length > 0)
            {
                T[] buffer = new T[array.Length + 1];

                Array.Copy(array, 0, buffer, 0, array.Length);

                buffer[array.Length] = value;

                return(buffer);
            }

            return(Params.Array(value));
        }