Adding to generic List

In this chapter you will learn:

  1. How to add elements to generic List

Adding to List

The following code shows how to add elements a List.

using System;//from   ja v a 2 s. 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");
        words.AddRange(new[] { "C", "D" });
        words.Insert(0, "A"); // Insert at start 
        words.InsertRange(0, new[] { "E", "F" });
        

    }

}

Next chapter...

What you will learn in the next chapter:

  1. What is the List capacity and element count