public static void CollectionDemo() { Int16Collection myI16 = new Int16Collection { 1, 2, 3, 5, 7 }; Console.WriteLine("Contents of the collection (using foreach):"); PrintValues1(myI16); Console.WriteLine("Contents of the collection (using enumerator):"); PrintValues2(myI16); Console.WriteLine("Initial contents of the collection (using Count and Item):"); PrintIndexAndValues(myI16); Console.WriteLine("Contains 3: {0}", myI16.Contains(3)); Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2)); Console.WriteLine(); myI16.Insert(3, 13); Console.WriteLine("Contents of the collection after inserting at index 3:"); PrintIndexAndValues(myI16); myI16[4] = 123; Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:"); PrintIndexAndValues(myI16); myI16.Remove(2); Console.WriteLine("Contents of the collection after removing the element 2:"); PrintIndexAndValues(myI16); }
public static void PrintIndexAndValues(Int16Collection myCol) { for (int i = 0; i < myCol.Count; i++) { Console.WriteLine(" [{0}]: {1}", i, myCol[i]); } Console.WriteLine(); }
public static void PrintValues1(Int16Collection myCol) { foreach (Int16 i16 in myCol) { Console.WriteLine(" {0}", i16); } Console.WriteLine(); }
public static void PrintValues2(Int16Collection myCol) { IEnumerator myEnumerator = myCol.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine(" {0}", myEnumerator.Current); } Console.WriteLine(); }