c# <lists>, generics and collections

The List collection class has properties similar to an array. The key difference is that a
List automatically expands as the number of elements increases while an array size is constant.


collections
Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key. A collection is a class, so you must declare a new collection before you can add elements to that collection. [msdn]

Lists
The List<T> collection class has properties similar to an array. Lists can expand automatically and can shrink via explicit calls to TrimToSize() or Capacity. Each list element can be individually accessed by an index, just like an array. Therefore, you can set and access elements in the list collection classes using the index operator, where the index parameter value corresponds to the position of an element in the collection. [michaelis]
 
   // [michaelis listing 16.2] Using List<T>
   using System;
   using System.Collections.Generic;
   class Program
   {
      static void Main()
      {
         // string is specified for generic type
         List<string> list = new List<string>();
         
         // Lists automatically expand as elements are added.
         list.Add("Sneezy");
         list.Add("Happy");
         list.Add("Dopey");
         list.Add("Doc");
         list.Add("Sleepy");
         list.Add("Bashful");
         list.Add("Grumpy");
         
         // sort is a method for a List
         list.Sort();
         
         // to console
         Console.WriteLine("In alphabetical order {0} is the first dwarf while {1} is the last.", 
                            list[0], list[6]); 
         
         // removes from List         
         list.Remove("Grumpy");
      }
   }
   
OUTPUT: In alphabetical order Bashful is the first dwarf while Sneezy is the last.

what is a generic?
A generic class has a parameter that is a type, like int or string. See List<T> where <T> indicates that when instantiated, an actual type will be substituted.
         List<int> twins = new List<int>();
   
This means you can provide only one implementation and call it with different types.

code: find twins in character array
    
      //---------------------------------------------
      // string of characters always in pairs (0&1, 2&3, etc)
      // find any twin pairs (aa, ff,zz, etc) and put into List which will be returned
      public List<int> FindTwins(char[] alphabet)
      {
         // array should have an even number of characters
         if(alphabet.Length % 2 == 0)
         {
            // return empty list
            twins.Clear();
            return twins;
         }  
      
         // create list to be filled and returned
         // hold indexes of array locations where twins occur
         // only look at every pair - array is even
         List<int> twins = new List<int>();
      
         // look at every second
         for (int i = 0; i < alphabet.Length; i += 2)
         {
            if (alphabet[i] == alphabet[i + 1])
            {
               // add location in array where twins occur
               twins.Add(i);  
            }
         }
         return twins;
      }