Generic Collections Common List(T) Operations using C# The List (T) is a strongly typed collection of objects that can
n be accessed by index. This class provides methods to loop, filter, sort and manipulate collections. The non-generic version of this class is the ArrayList class.
This article focuses on some common operations of Generics List(T) class, First of all create a console application , open Visual Studio 2008 /2010 File > New Project >Console Application
Here I am using collection of Product class. So first of all we will create Product class . Add a Product class to application, right click on project >Add > Class and rename it [Link] using using using using System; [Link]; [Link]; [Link];
namespace GenericCollectionsOperation { public class Product { private int productID; private string name; private string categoryName; private int price; #region Properties Region public int ProductID { get; set; } public string Name { get; set; } public string CategoryName { get; set; } public int Price { get; set; } #endregion } }
Now go to [Link] create Generic List Object [List(T)] and add Product objects to the List(T).
namespace GenericCollectionsOperation { class Program { static void Main(string[] args) { List<Product> products = new List<Product>() { new Product(){ProductID=1, =1,Name="Colgate", CategoryName="General" ,Price= 27}, new Product(){ProductID= 2 ,Name="Hair Oil",CategoryName="General" ,Price= 67}, new Product(){ProductID= 3 ,Name="Bottle",CategoryName="Crockery" ,Price=24}, new Product(){ProductID= 4 ,Name="Plate",CategoryName="Crockery" ,Price= 8}, new Product(){ProductID= 5 ,Name="Radio",CategoryName="Electronics" ,Price= 700}, new Product(){ProductID= 6 ,Name="BlueTooth",CategoryName="Electronics" ,Price= 1099} }; } } } Here We will Create a common Method to Print named PrintConsole .By Using this we can print List of Product items.
static void PrintOnConsole(List<Product> pList, string info) { [Link](info); [Link]("\n{0,5} {1,7} {2,10} {3,14} ", "ProductID", "Name", "Category", "Price"); [Link](delegate(Product product) { [Link]("{0,5} {1,9} {2,13} {3,14}", [Link], [Link], [Link], [Link]); });
Now we Focuses on List(T) operation s 1-Looping through all items of List of objects of Products.
Here we can call this common method for looping the items of List(Product) PrintOnConsole(productList, "Looping Through All Products in List"); [Link]();
Here is Output :
2-Filtering List(T) using a single condition - (ProductID > 3)
By writing this code we can filter List of products by ProductID List<Product> FilterbyID =[Link](delegate(Product product) { return [Link] > 3; }); PrintOnConsole(FilterbyID, "----Filtering List(T) by ProductID--------");
Here is Output:-
3. Filtering List(T) on multiple conditions (Price>20 && CategoryName == "Crockery")
List<Product> FilteredByTwo = [Link](delegate(Product prod) { return [Link] >20 && [Link] == "Crockery"; }); PrintOnConsole(FilteredByTwo, "----Filtering List(T) by Multiple Conditions(Price and CategoryName)-"); Here is Output:-
4. Sorting List(T) (Sorting by Name)
List<Product> SortByName = productList; [Link](delegate(Product prod1, Product prod2) { return [Link]([Link]); }); PrintOnConsole(SortByName, "----Sorting List(T) (Sorting by Name)-");
Here is output:_
6. Add new List(T) to existing List(T)
List<Product> NewList = new List<Product>() { new Product(){ProductID= 7 ,Name="Pepsi",CategoryName="Beverage" ,Price= 77}, new Product(){ProductID= 8 ,Name="Gilllete",CategoryName="General" ,Price= 333}, }; [Link](NewList); PrintOnConsole(productList, "Looping Through All Newly Added Products in List");
Here is Output:_