Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Myfuturz.wordpress.

com

Adding Mechanism of Generic List<T>


Table of Contents
How to Add item to the generic List<T>? ............................................................................................. 1 How to Add or Append Items to the existing generic List<T>? ............................................................ 1 How to Add or Append Collection of item to existing generic List<T>? ............................................... 2

1. How to Add item to the generic List<T>?


/// <summary> /// This is static collection used in this tutorials /// </summary> /// <returns></returns> private static List<CompanyProduct> AddCollection() { List<CompanyProduct> list = new List<CompanyProduct>(); list.Add(new CompanyProduct("MS Word", 1, "Microsoft", new DateTime(2012, 10, 31))); list.Add(new CompanyProduct("MS Excel", 2, "Microsoft", new DateTime(2011, 10, 31))); list.Add(new CompanyProduct("MS Powerpoint", 3, "Microsoft", new DateTime(2010, 10, 31))); list.Add(new CompanyProduct("Visual Studio", 2, "Microsoft", new DateTime(2011, 10, 31))); list.Add(new CompanyProduct("Sql Server", 3, "Microsoft", new DateTime(2010, 10, 31))); list.Add(new CompanyProduct("Oracle", 4, "Oracle", new DateTime(2011, 10, 31))); DisplayingList(list, "List of COllection"); return list; }

2. How to Add or Append Items to the existing generic List<T>?


/// <summary> /// This is used to add items to collection at once. /// </summary> private static void AppendingItems() { List<CompanyProduct> list = new List<CompanyProduct>(); list.Add(new CompanyProduct("Sharepoint", 6, "Microsoft", new DateTime(2012, 10, 31))); list.Add(new CompanyProduct("MS Outlook", 7, "Microsoft", new DateTime(2011, 10, 31))); List<CompanyProduct> existingItems = AddCollection(); existingItems.AddRange(list); //Display all the appended items. DisplayingList(existingItems, "Displaying Appending Item at once"); }

Myfuturz.wordpress.com

3. How to Add or Append Collection of item to existing generic List<T>?


/// <summary> /// This is used to add one by one item. /// </summary> private static void AppendingItem() { List<CompanyProduct> existingItems = AddCollection(); existingItems.Add(new CompanyProduct("Yahoo WebBlog", 4, "Yahoo", new DateTime(2012, 10, 31))); DisplayingList(existingItems, "Displaying Appending Item one by one"); }

You might also like