public static void Main() { // Create and initialize a new CollectionBase. Int16Collection myI16 = new Int16Collection(); // Add elements to the collection. myI16.Add((Int16)1); myI16.Add((Int16)2); myI16.Add((Int16)3); myI16.Add((Int16)5); myI16.Add((Int16)7); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine("Contents of the collection (using foreach):"); PrintValues1(myI16); // Display the contents of the collection using the enumerator. Console.WriteLine("Contents of the collection (using enumerator):"); PrintValues2(myI16); // Display the contents of the collection using the Count property and the Item property. Console.WriteLine("Initial contents of the collection (using Count and Item):"); PrintIndexAndValues(myI16); // Search the collection with Contains and IndexOf. Console.WriteLine("Contains 3: {0}", myI16.Contains(3)); Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2)); Console.WriteLine(); // Insert an element into the collection at index 3. myI16.Insert(3, (Int16)13); Console.WriteLine("Contents of the collection after inserting at index 3:"); PrintIndexAndValues(myI16); // Get and set an element using the index. myI16[4] = 123; Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:"); PrintIndexAndValues(myI16); // Remove an element from the collection. myI16.Remove((Int16)2); // Display the contents of the collection using the Count property and the Item property. Console.WriteLine("Contents of the collection after removing the element 2:"); PrintIndexAndValues(myI16); }
static void Main(string[] args) { Int16Collection myI16 = new Int16Collection(); myI16.Add((Int16)1); //加入2,3,5,7作为元素 Console.WriteLine("Initial contents of the collection:"); 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, (Int16)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((Int16)2); Console.WriteLine("Contents of the collection after removing the element 2:"); for (int i = 0; i < myI16.Count; i++) { Console.WriteLine(" [{0}]: {1}", myI16[i]); } }