Wednesday, January 30, 2013

C# TakeLast()


        ///
        ///     Remove and return the last item in the list, otherwise return null.
        ///
        ///
        ///
        ///
        public static TType TakeLast( this IList list ) {
            if ( list == null ) {
                throw new ArgumentNullException( "list" );
            }
            var index = list.Count - 1;
            if ( index < 0 ) {
                return default( TType );
            }
            var item = list[ index ];
            list.RemoveAt( index );
            return item;
        }

No comments: