Using C# to Program Generic .NET Interfaces
C# as a language is elegant and powerful, but to fully use its capabilities, you need to understand how it works within the .NET Framework. This article shows you how to make your C# classes work effectively with generic .NET interfaces.
This article is based on the books Introduction to C# Using .NET (Prentice Hall PTR, 2001, ISBN: 0130418013), by Robert J. Oberg; and Application Development Using C# and .NET (Prentice Hall PTR, 2001, ISBN: 013093383X), by Michael Stiefel and Robert J. Oberg. These books are part of The Integrated .NET Series from Object Innovations and Prentice Hall PTR.
You can download a full-length version of this article (Chapter 18 from Introduction to C# Using .NET) and sample code here.
Make Your C# Classes Work with the .NET Framework
The .NET Framework is a powerful class library. As such, there is a great deal of functionality (more than 2500 classes!) that you can call from your code. The basic organization of a library that supplies pre-written code that you can call is familiar from many programming environments. The fact that the .NET Framework is a class library means that the functions are grouped together into meaningful classes, and you can customize the behavior of the supplied classes by inheritance, creating your own derived classes.
To fully understandand exploitthe .NET Framework, you need to understand another key concept. Not only does your code call the Framework, but also the Framework calls your code. Your program can be viewed as the middle layer of a sandwich, as illustrated in Figure 1.
Figure 1 The .NET Framework sandwich.
There are several ways to make your classes plug into the architecture of the .NET Framework and thus be "good .NET citizens."
Override virtual methods of Framework classes
Implement delegates (or handle events) specified by Framework classes
Implement interfaces specified by the .NET Framework
Every class in C# inherits from the root class System.Object (in C#, you can use the keyword object for this root class). The class object has a basic set of methods:
- Object Constructor
If you are used to a language such as Smalltalk, the set of behaviors specified in object may seem quite limited. Smalltalk, which introduced the concept of a class hierarchy rooted in a common base class, has a very rich set of methods defined in its Object class. I counted 38 methods!
The methods of Smalltalk's Object class are described in Chapters 6 and 14 of Smalltalk-80: The Language and its Implementation (Addison-Wesley, 1989, ISBN: 0201136880), by Adele Goldberg and David Robson.
These additional methods support features such as comparing objects and copying objects. The .NET Framework class library has similar methods and many more. But rather than putting them all in a common root class, .NET defines a number of standard interfaces that classes can optionally support. This kind of organization, which is also present in Microsoft's Component Object Model (COM) and in Java, is very flexible.
In this article, we will show you how to use interfaces to perform the common operations of copying and comparing.