Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Working with generics in C#


We will create an IAnimal interface to specify the requirements that a type must meet in order to be considered an animal. We will create the Animal abstract base class that implements this interface. Then, we will specialize this class in two subclasses: Dog and Frog. Later, we will create the Party class that will be able to work with instances of any class that implements the IAnimal interface through generics. We will work with the party of dogs and frogs.

Now, we will create an IDeeJay interface and implement it in a HorseDeeJay class. We will also create a subclass of the Party class named PartyWithDeeJay that will use generics to work with instances of any type that implement the IAnimal interface and instances of any type that implements the IDeeJay interface. Then, we will work with the party of dogs with a DJ.

Declaring an interface to be used as a constraint

Now, it is time to code one of the interfaces that will be used as a constraint later when we...