Introduction:

In this article we will explain how to use generic List<T> in C# or what is generic List<T> in C# or why use generic List<T> in c# or how to add and browse data in generic List<T> c# with Example.


Description:

C# List<T> class in .NET represents a strongly typed list of objects that can be accessed by index. List class is a collection and defined in the System.Collections.Generic namespace and it provides most of the collections related built-in methods and properties including add, remove, search, and sort.  The Remove method is used to delete an item from the List. The Clear method can be used to clear all items from the list. The Contains method can be used to find an item.

The following example shows how to add items to a List and how to find an item using for-each loop in List Collection.

 Example of C# List<T>

Let's see an example of generic List<T> class that stores elements using Add() method and iterates the list using for-each loop.

 using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace collection
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a list of strings 
            var names = new List<string>();
            names.Add("Computer");
            names.Add("Mouse");
            names.Add("keyboard");
            names.Add("Mobile");
            names.Add("Pen drive");
 
            // Iterate list element using foreach loop 
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
            Console.ReadLine();
        }
    }
 
  
}


Output:

 

Example of C# List<T> using object

Let's see an example of generic List<T> class that stores object elements using Add() method and iterates the list using for-each loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace collection
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a list of object              var Products = new List<Product>();
            Product pro = new Product();
            pro.Name = "Computer";
            pro.Rate = 20000;
            Products.Add(pro);
            pro = new Product();
            pro.Name = "LAaptop";
            pro.Rate = 25000;
            Products.Add(pro);
            pro = new Product();
            pro.Name = "Mobile";
            pro.Rate = 10000;
            Products.Add(pro);
            pro = new Product();
            // Iterate list element using foreach loop 
            foreach (var product in Products)
            {
                Console.WriteLine("Product Name:" + product.Name + " " + "Rate:" + product.Rate);
            }
            Console.ReadLine();
        }
    }
 
    public class Product    {        public string Name { get; set; }        public decimal Rate { get; set; }    }
}


Output:

 

 

I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like