C# List Add

Description

List Add adds an object to the end of the List .

Syntax


public void Add(
  T item
)

Parameters

  • item - The object to be added to the end of the List . The value can be null for reference types.

Example

The following code shows how to add elements a List.


using System;//from ww w . ja va 2s.c  o  m
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        //Create a List of Strings
        //String-typed list
        List<string> words = new List<string>();  

        words.Add("A");
        words.Add("B");
        
        foreach (string s in words) {
            Console.WriteLine ("all:"+s);   // all words 
        }
    }

}

The code above generates the following result.





















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack