System.Collections.ArrayList.Insert C# (CSharp) Method

Insert() public method

public Insert ( int index, Object value ) : void
index int
value Object
return void
        public virtual void Insert(int index, Object value)
        {
            // Note that insertions at the end are legal.
            if (index < 0 || index > _size) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_ArrayListInsert);
            //Contract.Ensures(Count == Contract.OldValue(Count) + 1);
            Contract.EndContractBlock();

            if (_size == _items.Length) EnsureCapacity(_size + 1);
            if (index < _size)
            {
                Array.Copy(_items, index, _items, index + 1, _size - index);
            }
            _items[index] = value;
            _size++;
            _version++;
        }

Usage Example

Example #1
0
            /// <summary> Remove the given seqno and resize or partition groups as
            /// necessary. The algorithm is as follows:<br>
            /// i. Find the group with low <= seqno <= high
            /// ii. If seqno == low,
            /// a. if low == high, then remove the group
            /// Adjust global low. If global low was pointing to the group
            /// deleted in the previous step, set it to point to the next group.
            /// If there is no next group, set global low to be higher than
            /// global high. This way the entry is invalidated and will be removed
            /// all together from the pending msgs and the task scheduler
            /// iii. If seqno == high, adjust high, adjust global high if this is
            /// the group at the tail of the list
            /// iv. Else low < seqno < high, break [low,high] into [low,seqno-1]
            /// and [seqno+1,high]
            ///
            /// </summary>
            /// <param name="seqno">the sequence number to remove
            /// </param>
            public virtual void  remove(long seqno)
            {
                int  i;
                long loBound = -1;
                long hiBound = -1;

                lock (this)
                {
                    for (i = 0; i < list.Count; i += 2)
                    {
                        loBound = (long)list[i];
                        hiBound = (long)list[i + 1];

                        if (seqno < loBound || seqno > hiBound)
                        {
                            continue;
                        }
                        break;
                    }
                    if (i == list.Count)
                    {
                        return;
                    }

                    if (seqno == loBound)
                    {
                        if (loBound == hiBound)
                        {
                            list.RemoveAt(i);
                            list.RemoveAt(i);
                        }
                        else
                        {
                            list[i] = ++loBound;
                        }

                        if (i == 0)
                        {
                            low = list.Count == 0 ? high + 1:loBound;
                        }
                    }
                    else if (seqno == hiBound)
                    {
                        list[i + 1] = --hiBound;
                        if (i == list.Count - 1)
                        {
                            high = hiBound;
                        }
                    }
                    else
                    {
                        list[i + 1] = seqno - 1;

                        list.Insert(i + 2, hiBound);
                        list.Insert(i + 2, seqno + 1);
                    }
                }
            }
All Usage Examples Of System.Collections.ArrayList::Insert