示例#1
0
        /// <summary>
        /// 添加指定的值到数组中第一个未被占用的位置,超出范围时将不进行添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        /// <param name="val"></param>
        /// <returns>添加位置的索引值</returns>
        public static int AddElement <T>(this T[] arr, T val)
        {
            var index = arr.GetFreeIndex();

            if (index < arr.Length)
            {
                arr[index] = val;
                return((int)index);
            }
            return(-1);
        }
示例#2
0
        /// <summary>
        /// 添加指定的值到数组中第一个未被占用的位置,超出范围时重新分配数组大小为原大小+1
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static uint AddOnlyOne <T>(ref T[] arr, T val)
        {
            var index = arr.GetFreeIndex();

            // no free space found: Make more space
            if (index >= arr.Length)
            {
                EnsureSize(ref arr, (int)index + 1);
            }
            arr[index] = val;
            return(index);
        }