List<T>

The generic counterpart of the ArrayList type is the generic List<T> type, where the type parameter T stands for, unsurprisingly, the element type. Available methods on List<T> are pretty much the same as on ArrayList. We can even easily port our ArrayList example to List<int>:

var lst = new List<int>();lst.Add(1);                               // {    1 }lst.AddRange(new[] { 2, 3, 7, 6, 5, 4 }); // {    1, 2, 3, 7, 6, 5, 4 }lst.Insert(index: 0, item: 0);            // { 0, 1, 2, 3, 7, 6, 5, 4 }lst.RemoveAt(index: 4);                   // { 0, 1, 2, 3,    6, 5, 4 }lst.Remove(item: 1);                      // { 0,    2, 3,    6, 5, 4 }lst.RemoveRange(index: 2, count: 3);      // { 0,    2,             4 } ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.