Removes the last item of a IList. Deals with null IList - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Removes the last item of a IList. Deals with null IList

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;/* w  w  w  .  j  a  v  a2s . c o m*/
using Cirrious.CrossCore.Platform;

public class Main{
        /// <summary>
        /// Removes the last item of a IList.
        /// Deals with null IList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static bool SafeRemoveLast<T>(this IList<T> source)
        {
            try
            {
                if (source != null && source.Count > 0)
                {
                    source.RemoveAt(source.Count - 1);
                }

                return true;
            }
            catch (Exception ex)
            {
                MvxTrace.Error(ex.Message + Environment.NewLine + ex.StackTrace);
                return false;
            }
        }
}

Related Tutorials