O'Reilly logo

Learning C# 2005, 2nd Edition by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 14: Generics and Collections

Quiz

Solution to Question 14–1.

Indexers are unnamed. You use the this keyword to create an indexer:

public string this[int index]
Solution to Question 14–2.

Any type can be used, although it’s most common to use integers.

Solution to Question 14–3.

The elements of the array must implement IComparable.

Solution to Question 14–4.

Generics allow you to create type-safe collections without specifying the type the collection will hold when you create the collection.

Solution to Question 14–5.

It allows your collection to support a foreach loop.

Solution to Question 14–6.

The size of an array is fixed when you create it. A List<T> expands dynamically when you add more elements.

Solution to Question 14–7.

A List is like an expandable array. A Stack is a “Last In, First Out” collection, a Queue is a “First In, First Out” collection, and a Dictionary is a collection of key/value pairs where you can retrieve the value given a key.

Exercises

Solution to Exercise 14-1.

Create an abstract Animal class that has private members weight and name, and abstract methods Speak( ), Move( ), and ToString( ). Derive from Animal a Cat and Dog class that override the methods appropriately. Create an Animal array, populate it with Dogs and Cats, and then call each member’s overridden virtual method.

using System; abstract public class Animal { protected int weight; protected string name; public Animal(int weight, string name) { this.weight = weight; this.name = name; } abstract public void ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required