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

LastIndexOf() public method

public LastIndexOf ( Object value, int startIndex, int count ) : int
value Object
startIndex int
count int
return int
        public virtual int LastIndexOf(Object value, int startIndex, int count)
        {
            if (Count != 0 && (startIndex < 0 || count < 0))
                throw new ArgumentOutOfRangeException(startIndex < 0 ? nameof(startIndex) : nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            Contract.Ensures(Contract.Result<int>() < Count);
            Contract.EndContractBlock();

            if (_size == 0)  // Special case for an empty list
                return -1;

            if (startIndex >= _size || count > startIndex + 1)
                throw new ArgumentOutOfRangeException(startIndex >= _size ? nameof(startIndex) : nameof(count), SR.ArgumentOutOfRange_BiggerThanCollection);

            return Array.LastIndexOf((Array)_items, value, startIndex, count);
        }

Same methods

ArrayList::LastIndexOf ( Object value ) : int
ArrayList::LastIndexOf ( Object value, int startIndex ) : int

Usage Example

Example #1
0
        public void TestLastIndexOfBasic()
        {
            //--------------------------------------------------------------------------
            // Variable definitions.
            //--------------------------------------------------------------------------
            ArrayList arrList = null;
            int ndx = -1;
            //
            //  Construct array lists.
            //
            arrList = new ArrayList((ICollection)strHeroes);

            //
            // []  Obtain last index of "Batman" items.
            //
            ndx = arrList.LastIndexOf("Batman");
            if (ndx != -1)
            {
                Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx]));
            }

            //
            // []  Attempt to find null object.
            //
            // Remove range of items.
            ndx = arrList.LastIndexOf(null);
            Assert.Equal(-1, ndx);

            // []  Call LastIndexOf on an empty list
            var myList = new ArrayList();
            var lastIndex = myList.LastIndexOf(6);

            Assert.Equal(-1, lastIndex);
        }
All Usage Examples Of System.Collections.ArrayList::LastIndexOf