Anda di halaman 1dari 82
How to work with interfaces and generics This chapter starts by showing you how to use interfaces. Interfaces are similar to abstract classes, but they have several advantages that make them easier to create and more flexible to use. Then, this chapter shows you how to use generics so you can code your own collections that work like the generic, collections from the NET Framework presented in chapter 8. Along the way, you'll learn how to work with the generic interfaces that are used with generic collections. How to work with interfaces 462 ‘An introduction to interfaces 462 Some of the interfaces defined by the NET Framework 464 How to ereate an interface 465 How to implement aa interface 468. ‘A Product class that implements the ICloneable interface 470 How to use an interface as a parameter an How to work with generics ... enone ATA How to code a clas that defines a generic collection 44 Some of the generic interfaces defined by the NET Framework 418 How to implement the IComparable<> interface 430 How to use constraints 482 How to implement the [Enumerable<> interface 434 How to code an interface that uses generics 485 Perspective... cone ABB 462 Section 3 Object-oriented programming How to work with interfaces In some object-oriented programming languages, such as C++ and Perl, a class can inherit more than one class. This is known as multiple inkeritance. In C#, however, a class can inherit only one class. Although C# doesn’t support multiple inheritance, it does support a special type of coding element known as an interface. An interface provides many of the advantages of multiple inheritance without some of the problems that are associ- ated with it, In the topics that follow, you'll learn how to work with interfaces. An introduction to interfaces In some ways, an interface is similar to an abstract class. That's why figure 15-1 compares interfaces to abstract classes. To start, abstract classes and inter- faces can both include one or more members that aren't implemented. In the case of an abstract class, the implementation for these members must be provided by any subclass that inherits the abstract class. Similarly, the implementation for the ‘members of an interface must be included in any class that implements the interface. The difference is that an interface can’t include the implementation for any of its members, but an abstract class can. ‘An important difference between abstract classes and interfaces is that a C# class can inherit only one class (abstract or not), but it can implement more than one interface. This is how C# interfaces can be used to provide some of the features of multiple inheritanc: The examples in this figure show how a simple interface is declared and implemented by a class. The first example shows the declaration for a custom interface named IDisplayable. This interface includes a single method named GetDisplayText that allows an object to return a string that can be used to display the object. Any class that implements the IDisplayable interface must provide an implementation of the GetDisplayText method. The second example shows a simplified version of a Product class that implements the IDisplayable interface. Here, you can sce that the class statement for the Product class lists IDisplayable as an interface that’s implemented by the class. Then, the class provides an implementation of the GetDisplayText method that returns a string that contains all three properties of the Product class. The third example shows that a Product object that implements the IWisplayable interface can be stored in a variable of the IDisplayable type. In other words, an object created from a Product class that implements the IDisplayable interface is both a Product object and an IDisplayable object. As a result, you can use this object anywhere an IDisplayable object is expected, In this figure, the interface name begins with the capital letter I. Although that’s not a requirement, it’s a coding convention that’s followed by all the interfaces in the NET Framework, and it helps you distinguish between inter faces and classes. So when you create your own interfaces, we recommend that you follow this coding convention, Chapter 15 How to work with interfaces and generics 463 The IDisplayable interface interface IDisplayable ‘ string GetpisplayText (string sep); ? A Product class that implements the IDisplayable public class Product : rDisplayable « public string code ( get; set; ) public string Description ( get public decimal Price ( get; set; ) ts) public Product (string Code, string Description, decimal Price) « this.code = code this-Deseription = Description; this.Price = Price; > public string GetDisplayText (string « >) return this.code + sep + this.Description + sep + this.Price.tostring("c"); > Code that uses the IDisplayable interface qDisplayable product = new Product ("cs12", "Murach's C# 2012", 54.5m); Console.WriteLine (product .GetDisplayText ("\n")); A comparison of interfaces and abstract classes + Both interfaces and abstract class must implement. lasses provide signatures for properties and methods that a + Allof the members of an interface are abstract. In contrast, an abstract class can imple- ment some or all of its members. ‘+A class can inherit only one class (including abstract classes), but a class can implement more than one interface. ‘+ Interfaces can’t declare static members, but abstract classes can, Description + An interface consists of a set of signatures for one or more methods, properties, index- cers, or events, An interface doesn't provide an implementation for any of its members. Instead, it indicates what members must be defined by any class that implements the interface. ‘+ By convention, interface names begin with the letter Ito distinguish them from classes. ‘+ To implement an interface, a class must name the interface on the class declaration, and. it must provide an implementation for every member of the interface. Figure 15-1 An introduction to interfaces

Anda mungkin juga menyukai