Home > DeveloperSection > Forums > How to use C# List Class
Ankit Singh

Total Post:341

Points:2389
Posted on    November-05-2015 10:07 PM

 .NET C# 
Ratings:


 1 Reply(s)
 262  View(s)
Rate this:
I want to use list class in  c#.please help me


aditya kumar Patel

Total Post:242

Points:1722
Posted on    November-05-2015 10:15 PM

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; }

    }

}

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick