Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Basic operations on a Collection of characters.Lecture 12 - slide 9 : 36
Program 1
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

class BasicCollectionDemo{

  public static void Main(){

    // Initialization - use of a collection initializer. After that add 2 elements.
    IList<char> lst = new Collection<char>{'a', 'b', 'c'};
    lst.Add('d'); lst.Add('e'); 
    ReportList("Initial List", lst);                  

    // Mutate existing elements in the list:
    lst[0] = 'z'; lst[1]++; 
    ReportList("lst[0] = 'z'; lst[1]++;", lst);       

    // Insert and push towards the end:
    lst.Insert(0,'n'); 
    ReportList("lst.Insert(0,'n');", lst);            

    // Insert at end - with Insert:
    lst.Insert(lst.Count,'x');      // equivalent to lst.Add('x');
    ReportList("lst.Insert(lst.Count,'x');", lst);    

    // Remove element 0 and pull toward the beginning:
    lst.RemoveAt(0);
    ReportList("lst.RemoveAt(0);", lst);              

    // Remove first occurrence of 'c':
    lst.Remove('c'); 
    ReportList("lst.Remove('c');", lst);              

    // Remove remaining elements:
    lst.Clear(); 
    ReportList("lst.Clear(); ", lst);                 

  }

  public static void ReportList<T>(string explanation, IList<T> list){
    Console.WriteLine(explanation);
    foreach(T el in list)
      Console.Write("{0, 3}", el);
    Console.WriteLine(); Console.WriteLine();
  }

}
 
 
 
 
 
 
 
 
 
 
 
a b c d e
 
 
 
z c c d e
 
 
 
n z c c d e
 
 
 
n z c c d e x
 
 
 
z c c d e x
 
 
 
z c d e x
 
 
 
The list is now empty.