This document discusses three ways to add items to a generic List in C#:
1. To add a single item, use the List.Add() method, passing the item to add.
2. To add multiple items at once, add the items to a separate list, then use List.AddRange() to append the separate list to the existing list.
3. To add one item at a time, use List.Add() and pass each individual item.
This document discusses three ways to add items to a generic List in C#:
1. To add a single item, use the List.Add() method, passing the item to add.
2. To add multiple items at once, add the items to a separate list, then use List.AddRange() to append the separate list to the existing list.
3. To add one item at a time, use List.Add() and pass each individual item.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
This document discusses three ways to add items to a generic List in C#:
1. To add a single item, use the List.Add() method, passing the item to add.
2. To add multiple items at once, add the items to a separate list, then use List.AddRange() to append the separate list to the existing list.
3. To add one item at a time, use List.Add() and pass each individual item.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
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"); }