Generic Collections – Common List(T) Operations using C#

C#

The List (T) is a strongly typed collection of objects in Generic Collections that can 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 Product.cs .

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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
}
}
[/csharp]

Now go to Program.cs  create Generic List Object [List(T)]  and add Product objects to the List(T).

[csharp]
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}
};

}
}
}
[/csharp]

Here We will Create a common  Method to Print named PrintConsole .By Using this we can print  List of Product items.

[csharp]
static void PrintOnConsole(List<Product> pList, string info)
{
Console.WriteLine(info);
Console.WriteLine("\n{0,5} {1,7} {2,10} {3,14}",
"ProductID", "Name", "Category", "Price");
pList.ForEach(delegate(Product product)
{
Console.WriteLine("{0,5} {1,9} {2,13} {3,14}", product.ProductID, product.Name, product.CategoryName,    product.Price);

});
}
[/csharp]

Now we Focuses on List(T) operations:

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");
Console.Read();

Here is Output :

collection

2-Filtering List(T) using a single condition - (ProductID > 3):

By writing this code we can filter List of products by ProductID

[csharp]
List<Product> FilterbyID = productList.FindAll(delegate(Product product)
{
return product.ProductID > 3;
});
PrintOnConsole(FilterbyID, "----Filtering List(T) by ProductID--------");
[/csharp]

Here is Output:-

collection3. Filtering List(T) on multiple conditions (Price>20 && CategoryName == "Crockery"):

[csharp]
List<Product> FilteredByTwo = productList.FindAll(delegate(Product prod)
{
return prod.Price >20 && prod.CategoryName == "Crockery";
});
PrintOnConsole(FilteredByTwo, "----Filtering List(T) by Multiple Conditions(Price and CategoryName)-");
[/csharp]

Here is Output:-

collection

4. Sorting List(T) (Sorting by Name):

[csharp]
List<Product> SortByName = productList;
SortByName.Sort(delegate(Product prod1, Product prod2)
{
return prod1.Name.CompareTo(prod2.Name);
});
PrintOnConsole(SortByName, "----Sorting List(T) (Sorting by Name)-");
[/csharp]

Here is output:-

collection

5. Add new List(T) to existing List(T):

[csharp]
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},

};
productList.AddRange(NewList);
PrintOnConsole(productList, "Looping Through All Newly Added Products in List");
[/csharp]

Here is Output:-

collection
Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

4 comments:

  1. Reallly very well explained .Thanks a lot

    ReplyDelete
  2. Very simple explanation..good read

    ReplyDelete
  3. It is very helpful article!!!!!!!!!!!!!!!!!!!

    ReplyDelete