Adds an item to list if it is not already in the list. Deals with null IList - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Adds an item to list if it is not already in the list. Deals with null IList

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//www. j  a  va 2s.  c  om
using Cirrious.CrossCore.Platform;

public class Main{
        /// <summary>
        /// Adds an item to list if it is not already in the list.
        /// Deals with null IList
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="itemToAdd">The item to add.</param>
        public static void SafeAddMissing(this IList source, object itemToAdd)
        {
            if (source == null)
                return;

            if (!source.Contains(itemToAdd))
                source.Add(itemToAdd);
        }
}

Related Tutorials