// Detach other sink from events.
Console.WriteLine("\n***** Removing second sink *****");
c1.Unadvise(myOtherSink);
Console.ReadLine();
}
Callback interfaces can be helpful in that they can be used under any language or platform
(.NET, J2EE, or otherwise) that supports interface-based programming. However, as mentioned,
Chapter 11 will examine a number of event-centric techniques that are specific to the .NET
platform.
■
Source Code
The CallbackInterface project is located under the Chapter 9 subdirectory.
Summary
An interface can be defined as a named collection of abstract members. Because an interface does
not provide any implementation details, it is common to regard an interface as a behavior that may
be supported by a given type. When two or more classes implement the same interface, you are able
to treat each type the same way (aka interface-based polymorphism) even if the types are defined
within unique class hierarchies.
C# provides the interface keyword to allow you to define a new interface. As you have seen, a
type can support as many interfaces as necessary using a comma-delimited list. Furthermore, it is
permissible to build interfaces that derive from multiple base interfaces.
In addition to building your custom interfaces, the .NET libraries define a number of standard
(i.e., framework-supplied) interfaces. As you have seen, you are free to build custom types that
implement these predefined interfaces to gain a number of desirable traits such as cloning, sorting,
and enumerating. Finally, you spent some time investigating how interface types can be used to
establish bidirectional communications between two objects in memory.
C H A P T E R 9
■
W O R K I N G W I T H I N T E R FA C E S
308
8849CH09.qxd 10/1/07 10:41 AM Page 308
Collections and Generics
T
he most primitive container within the .NET platform is the System.Array type. As you have seen
over the course of the previous chapters, C# arrays allow you to define a set of identically typed
items (including an array of System.Objects, which essentially represents an array of any types) of
a fixed upper limit. While this will often fit the bill, there are many other times where you require
more flexible data structures, such as a dynamically growing and shrinking container, or a container
that can hold only items that meet a specific criteria (e.g., only items deriving from a given base
class, items implementing a particular interface, or whatnot). To begin understanding the task of
building flexible and type-safe containers, this chapter will first examine the System.Collections
namespace that has been part of the .NET base class libraries since the initial release.
However, since the release of .NET 2.0, the C# programming language was enhanced to sup-
port a new feature of the CTS termed
generics. Many of the generics you will use on a daily basis are
found within the System.Collections.Generic namespace. As shown over this chapter, generic con-
tainers are in many ways far superior to their nongeneric counterparts in that they provide greater
type safety and performance benefits. Once you’ve seen generic support within the base class
libraries, in the remainder of this chapter you’ll examine how you can build your own generic
members, classes, structures, and interfaces.
Do'stlaringiz bilan baham: |