List(T) Class represents a strongly typed list of objects that can be accessed by index.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> myList = new List<string>();
Console.WriteLine("\nCapacity: {0}", myList.Capacity);
myList.Add("T");
myList.Add("A");
myList.Add("M");
myList.Add("D");
myList.Add("C");
foreach(string d in myList)
{
Console.WriteLine(d);
}
Console.WriteLine("Capacity: {0}", myList.Capacity);
Console.WriteLine("Count: {0}", myList.Count);
Console.WriteLine(myList.Contains("D"));
myList.Insert(2, "C");
foreach(string d in myList)
{
Console.WriteLine(d);
}
Console.WriteLine( myList[3]);
myList.Remove("C");
foreach(string d in myList)
{
Console.WriteLine(d);
}
myList.TrimExcess();
Console.WriteLine("Capacity: {0}", myList.Capacity);
Console.WriteLine("Count: {0}", myList.Count);
myList.Clear();
Console.WriteLine("Capacity: {0}", myList.Capacity);
Console.WriteLine("Count: {0}", myList.Count);
}
}
Related examples in the same category