Now let's see how to do various types of mechanisms like adding, removing and inserting.

  1. How to Add an 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");

    }

  3. How to Add or Append a Collection of items to an 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");

    }

  4. How to Remove an item from the generic List<T>?

    /// <summary>

    /// Removes the specified Single item from the list.

    /// </summary>

    private static void RemoveItem()

    {
    CompanyProduct
    removeSharepoint = new CompanyProduct("Sharepoint", 6, "Microsoft", new DateTime(2012, 10, 31));
    CompanyProduct removeOutlook = new CompanyProduct("MS Outlook", 7, "Microsoft", new DateTime(2011, 10, 31));

    //Existing Items

    List<CompanyProduct> existingItems = AddCollection();

    //It removes the sharepoint product from list.So whatever companyProduct you sends

    // it just remove from the list.

    existingItems.Remove(removeSharepoint);

    existingItems.Remove(removeOutlook);

    DisplayingList(existingItems, "Removing Item one by one");

    }

  5. How to Remove the specific item from the generic List<T>?

    /// <summary>

    /// Removes the specified item by index.

    /// </summary>

    private static void RemoveSpecificItem()

    {

    //Existing Items

    List<CompanyProduct> existingItems = AddCollection();

    //It removes the sharepoint product from list.So whatever companyProduct you sends

    // it just remove from the list.

    existingItems.RemoveAt(1); // Removed MS Word

    existingItems.RemoveAt(3); // Removed MS PowerPoint

    DisplayingList(existingItems, "Removing Item one by one");

    }

  6. How to Remove the range of items from the generic List<T>?

    /// <summary>

    /// This is used to remove the specific range of items

    /// </summary>

    private static void RemoveSpecificRangeOfItems()

    {

    List<CompanyProduct> existingItems = AddCollection();

    //it removes first item from the list.

    existingItems.RemoveRange(1, 1);

    //it starts from second element and remove 2nd and 3rd item form the list.

    existingItems.RemoveRange(2, 2);

    DisplayingList(existingItems, "Removed Items");

    }

  7. How to Clear items from the generic List<T>?

    /// <summary>

    /// This is used to remove all the elements form the list.

    /// </summary>

    private static void CleareAllItem()

    {

    List<CompanyProduct> existingItems = AddCollection();

    existingItems.Clear();

    Console.WriteLine("count of List=", existingItems.Count);

    }

  8. How to Insert an item to the Generic List<T>?

    /// <summary>

    /// This is used to insert item at the last of collection.

    /// </summary>

    private static void InsertItem()

    {

    CompanyProduct addnotepad = new CompanyProduct("Notepad ", 7, "Unknown",

    new DateTime(2011, 10, 31));

    //Existing Items

    List<CompanyProduct> existingItems = AddCollection();

    //Inserted the norepad product at index 1.

    existingItems.Insert(1, addnotepad);

    DisplayingList(existingItems, "Displaying Inserted Item");

    }

  9. How to Insert items at a specified range to the Generic List<T>?

    /// <summary>

    /// This is used to insert item at specified range.

    /// </summary>

    private static void InsertRangeItem()

    {

    List<CompanyProduct> list = new List<CompanyProduct>();

    list.Add(new CompanyProduct("LinqPad", 1, "Unknown",

    new DateTime(2012, 10, 31)));

    list.Add(new CompanyProduct("Silverlight", 2, "Microsoft",

    new DateTime(2011, 10, 31)));

    //Existing Items

    List<CompanyProduct> existingItems = AddCollection();

    //Inserted the norepad product at index 1.

    existingItems.InsertRange(3, list);

    DisplayingList(existingItems, "Displaying Inserted Collection");

    }

Note: Download the source and debug the code, it helps to understand the exact operations with the List.

Thanks for reading this article. Have a nice day.