The following code demonstrates List's properties and methods.
using System; using System.Collections.Generic; class MainClass// w w w . j a v a2s . co m { public static void Main(string[] args) { List<string> words = new List<string>(); // New string-typed list words.Add ("A"); words.Add ("B"); words.AddRange (new[] { "C", "D" } ); words.Insert (0, "E"); // Insert at start words.InsertRange (0, new[] { "F", "G" }); // Insert at start words.Remove ("A"); words.RemoveAt (3); // Remove the 4th element words.RemoveRange (0, 2); // Remove first 2 elements // Remove all strings starting in 'n': words.RemoveAll (s => s.StartsWith ("n")); Console.WriteLine (words [0]); // first word Console.WriteLine (words [words.Count - 1]); // last word foreach (string s in words) Console.WriteLine (s); // all words List<string> subset = words.GetRange (1, 2); // 2nd->3rd words string[] wordsArray = words.ToArray(); // Creates a new typed array // Copy first two elements to the end of an existing array: string[] existing = new string [1000]; words.CopyTo (0, existing, 998, 2); List<string> upperCastWords = words.ConvertAll (s => s.ToUpper()); List<int> lengths = words.ConvertAll (s => s.Length); } }