Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
152 views5 pages

Generic Collections - Common List (T) Operations Using C#: Product

This document discusses common operations that can be performed on generic List(T) collections in C#, including looping through items, filtering by single or multiple conditions, sorting, and adding new lists. It provides code examples to demonstrate how to create a List of custom Product objects, print the list contents, filter the list based on ID or price/category, sort by name, and add a new list of items.

Uploaded by

Ram Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views5 pages

Generic Collections - Common List (T) Operations Using C#: Product

This document discusses common operations that can be performed on generic List(T) collections in C#, including looping through items, filtering by single or multiple conditions, sorting, and adding new lists. It provides code examples to demonstrate how to create a List of custom Product objects, print the list contents, filter the list based on ID or price/category, sort by name, and add a new list of items.

Uploaded by

Ram Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 Product.cs using using using using System; System.Collections.Generic; System.Linq; 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 } }

Now go to Program.cs 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) { 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); });

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

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 =productList.FindAll(delegate(Product product) { return product.ProductID > 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 = productList.FindAll(delegate(Product prod) { return prod.Price >20 && prod.CategoryName == "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; SortByName.Sort(delegate(Product prod1, Product prod2) { return prod1.Name.CompareTo(prod2.Name); }); 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}, }; productList.AddRange(NewList); PrintOnConsole(productList, "Looping Through All Newly Added Products in List");

Here is Output:_

You might also like