C# List List(Int32)

Description

List List (Int32) initializes a new instance of the List class that is empty and has the specified initial capacity.

Syntax


public List(
  int capacity
)

Parameters

  • capacity - The number of elements that the new list can initially store.

Example

The following example demonstrates the List(Int32) constructor.


//  w  w  w . j  a  v  a  2  s.c  om
using System;
using System.Collections.Generic;

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

        Console.WriteLine("\nCapacity: {0}", myData.Capacity);

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

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack