Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.81 KB, 2 trang )
The Problem with Objects
In order to understand Generics, it is worth looking in detail at the problems they are
designed to solve, specifically when using the object type.
You can use the object type as a reference to any type of value or variable. All reference
types automatically inherit (either directly or indirectly) from the System.Object class in
the .NET Framework. You can use this information to create highly generalized classes
and methods. For example, many of the classes in the System.Collections namespace
exploit this fact to allow you to create collections of any type. You will also notice in the
System.Collections.Queue class that you can create queues containing almost anything
(you have already been introduced to the collection classes in Chapter 10, “Using Arrays
and Collections”). The following fragment shows how to create and manipulate a queue
of Circle objects:
using System.Collections;
...
Queue myQueue = new Queue();
Circle myCircle = new Circle();
myQueue.Enqueue(myCircle);
...
myCircle = (Circle)myQueue.Dequeue();
The Enqueue method adds an object to the head of a queue, and the Dequeue method
removes the object at the other end of the queue. These methods are defined like this:
public void Enqueue( object item );
public object Dequeue();
Because the Enqueue and Dequeue methods manipulate objects, you can operate on
queues of Circles, PhoneBooks, Clocks, or any of the other classes you have seen in
earlier exercises in this book. However, it is important to notice that you have to cast the
value returned by the Dequeue method to the appropriate type because the compiler will
not perform the conversion from the object type automatically. If you don't cast the
returned value, you will get the compiler error “Cannot implicitly convert type 'object' to