Collections C# Book

The followoing code is the structure of List class.

public class List : IList
{
public List ();
public List (IEnumerable collection);
public List (int capacity);

// Add+Insert
public void Add (T item);
public void AddRange (IEnumerable collection);
public void Insert (int index, T item);
public void InsertRange (int index, IEnumerable collection);

// Remove
public bool Remove (T item);
public void RemoveAt (int index);
public void RemoveRange (int index, int count);
public int RemoveAll (Predicate match);

// Indexing
public T this [int index] { get; set; }
public List GetRange (int index, int count);
public Enumerator GetEnumerator();

// Exporting, copying and converting:
public T[] ToArray();
public void CopyTo (T[] array);
public void CopyTo (T[] array, int arrayIndex);
public void CopyTo (int index, T[] array, int arrayIndex, int count);

public ReadOnlyCollection AsReadOnly();
public List ConvertAll (Converter converter);
// Other:
public void Reverse();
public int Capacity { get;set; }
public void TrimExcess();
public void Clear();
}

The following code shows how to add elements a List.

using System;
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List words = new List();
words.Add("A");
words.Add("B");
words.AddRange(new[] { "C", "D" });
words.Insert(0, "A"); // Insert at start
words.InsertRange(0, new[] { "E", "F" });

}
}

Remove elements from a List

using System;
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List words = new List();
words.Add("A");
words.Add("B");
words.Add("C");
words.Add("D");
words.Add("E");
words.Add("F");
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"));
}
}

Get elements from List

using System;
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List words = new List();
words.Add("A");
words.Add("B");
words.Add("C");
words.Add("D");
words.Add("E");
words.Add("F");
Console.WriteLine("First:"+words[0]); // first word
Console.WriteLine ("Last:"+words[words.Count - 1]); // last word
foreach (string s in words)
Console.WriteLine ("all:"+s); // all words
IList subset = words.GetRange (1, 2); // 2nd->3rd words
}
}

The output:
First:A
Last:F
all:A
all:B
all:C
all:D
all:E
all:F
Convert List to an Array

using System;
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List words = new List();
words.Add("A");
words.Add("B");
words.Add("C");
words.Add("D");
words.Add("E");
words.Add("F");
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);
}
}
Convert List element

using System;
using System.Collections;
using System.Collections.Generic;
class Sample
{
public static void Main()
{
//Create a List of Strings
//String-typed list
List words = new List();
words.Add("A");
words.Add("B");
words.Add("C");
words.Add("D");
words.Add("E");
words.Add("F");
List upperCastWords = words.ConvertAll(s => s.ToUpper());
List lengths = words.ConvertAll(s => s.Length);
}
}