Returns a copy of a collection with a new single item added to it. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Returns a copy of a collection with a new single item added to it.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w w  w.  j a  va2s  . com*/

public class Main{
        /// <summary>
        /// Returns a copy of a collection with a new single item added to it.
        /// </summary>
        public static IEnumerable<T> Append<T>(this IEnumerable<T> lhs, T rhs)
        {
            List<T> list = lhs.ToList();
            list.Add(rhs);
            return list;
        }
        /// <summary>
        /// Returns a copy of a collection with the contents of another collection appended to it.
        /// </summary>
        public static IEnumerable<T> Append<T>(this IEnumerable<T> lhs, IEnumerable<T> rhs)
        {
            List<T> list = lhs.ToList();
            list.AddRange(rhs);
            return list;
        }
}

Related Tutorials