Adds a new item to ICollection with the default constructor - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Adds a new item to ICollection with the default constructor

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*from  w ww  .j  av  a  2 s  .c  o m*/

public class Main{
        /// <summary>
        ///     Adds a new item with the default constructor
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <returns></returns>
        public static T AddNew<T>(this ICollection<T> items)
            where T : new()
        {
            var item = new T();

            items.Add(item);

            return item;
        }
}

Related Tutorials