Now let's see how to do various types of mechanisms like adding, removing and inserting.
- 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;
}
-
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");
}
-
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");
}
-
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");
}
-
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");
}
-
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");
}
-
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);
}
-
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");
}
-
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.

moe satPosted Nov 19, 2013, 10:47 PM
http://www.c-sharpcorner.com/Forums/Thread/238227/in-looping-cannt-remove-and-insert-arraylist.aspx Can u give me some advise please read this post.I have some problem :-(
Lajapathy ArunPosted May 6, 2012, 1:48 AM
Thanks priya
Shanmuga Priya VPosted May 5, 2012, 2:38 PM
This helped me thank you...
Shubham SrivastavaPosted Apr 8, 2012, 7:09 AM
can we use generic list<T> as a data source for Linq ?.