s

C# List edit button Edit

author
Rakesh Kumar Sutar | calendar 20 January 2021 | 3498

Introduction

List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists.

Characteristics

  • It is different from the arrays. A List<T> can be resized dynamically but arrays cannot.
  • List<T> class can accept null as a valid value for reference types and it also allows duplicate elements.
  • If the Count becomes equals to Capacity, then the capacity of the List increased automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
  • List<T> class is the generic equivalent of ArrayList class by implementing the IList<T> generic interface.
  • This class can use both equality and ordering comparer.
  • List<T> class is not sorted by default and elements are accessed by zero-based index.
  • For very large List<T> objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the configuration element to true in the run-time environment.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    
    class Program
    {
        
        static void Main(String[] args)
        {
            // Creating a List of integers  
            // Capacity explicitly 
            List firstlist = new List();
            // adding elements in firstlist 
            firstlist.Add(1);
            firstlist.Add(2);
            firstlist.Add(3);
            firstlist.Add(4);
            Console.WriteLine("Capacity Is: "   firstlist.Capacity);
            Console.WriteLine("Count Is: "   firstlist.Count);
            firstlist.Add(5);
            firstlist.Add(6);
            Console.WriteLine("Capacity Is: "   firstlist.Capacity);
            Console.WriteLine("Count Is: "   firstlist.Count);
            Console.ReadLine();
        }
    }
}

Output