Create a List that is empty and has the specified initial capacity in CSharp

Description

The following code shows how to create a List that is empty and has the specified initial capacity.

Example


using System;//from  ww  w . j  av a  2  s .  c  o  m
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> myData = new List<string>(4);

        Console.WriteLine(myData.Capacity);

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");


        Console.WriteLine(myData.Capacity);

        myData.Add("java2s.com");

        foreach(string s in myData)
        {
            Console.WriteLine(s);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary