HOW TO USE C# LIST CLASS

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C# 
 605  View(s)
Ratings:
Rate this:
I want to use list class in  c#.please help me
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use C# List Class

    The Collection classes are a group of classes designed specifically for grouping together objects and performing tasks on them. List class is a collection and defined in the System.Collections.Generic namespace and it provides the methods and properties like other Collection classes such as add, insert, remove, search etc.

    The C# List < T > class represents a strongly typed list of objects that can be accessed by index and it supports storing values of a specific type without casting to or from object.

     

    List < T >

     

    The parameter T is the type of elements in the list.

     

     

    
    
    

    using System;

    using System.Collections.Generic;

    using System.Diagnostics;

    using System.Linq;

    using System.Net;

    using System.Text;

    using System.Threading;

    namespace Forum

    {

        public class ChildClass

        {

            static void Main(string[] args)

            {

    // You can add items int List collection.

                List<Customer> CustomerList = new List<Customer>();

                Customer obj = new Customer();

                obj.CustomerID = 1;

                obj.CustomerName = "Visal";

                obj.ContactNumber = "8787878787";

                CustomerList.Add(obj);

                obj = new Customer();

                obj.CustomerID = 1;

                obj.CustomerName = "Visal";

                obj.ContactNumber = "9797979797";

                CustomerList.Add(obj);

                obj = new Customer();

                obj.CustomerID = 1;

                obj.CustomerName = "Visal";

                obj.ContactNumber = "77986521322";

                CustomerList.Add(obj);

    // You can retrieve items from List collection by using for loops.

                for (int i = 0; i < CustomerList.Count; i++)

                {

                    Console.WriteLine("CustomerID: {0}", CustomerList[i].CustomerID);

                    Console.WriteLine("CustomerName:{0}", CustomerList[i].CustomerName);

                    Console.WriteLine("ContactNumber:{0}", CustomerList[i].ContactNumber +"\n"); 

                }

    Console.ReadLine();

            }

        }

        public class Customer

        {

            public int CustomerID { get; set; }

            public String CustomerName { get; set; }

            public string  ContactNumber { get; set; }

        }

    }

     

     

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!