Summary


This chapter focused on the usage of inheritance in our code, that is, using implementation inheritance to write virtual methods in the base class, by using the virtual keyword declaration. Overriding virtual methods in the derived class by using the override keyword was discussed. We looked at the MSIL and discovered many keywords defining the implementation of inheritance, including newslot and callvirt.

We discussed the merits of using abstract base classes over concrete base classes. We also saw that the ultimate base class was the System.Object, which even structs (which derive from System.ValueType) have as their ultimate base class.

We saw virtual and non-virtual dispatching, including the use of the new keyword and emphasized hiding of inherited members. We saw how to register the expected behavior of the inherited classes using polymorphism by overriding methods in the derived class, or accessing base-class methods from a base-class reference to a derived-class object instance.

We also discussed the concept of shadowing and to how to add members to the class with the same method (or property) name as the inherited or overridden method, but with a different signature allowing us to specify a series of overloaded methods without changing the interface on the base class.

We discussed how each class had its own method table with contiguous regions for inherited virtuals and its own virtuals in vtables. This is used by an overridden method to overwrite the memory address of the inherited virtual method in the vtable.

We saw how to use interfaces and multiple interface inheritance and how the interface map identifies the interfaces that the concrete class supports. IL explains the operations and how C# code is compiled and supported by the CLR. We identified the use of multiple interface inheritance and saw how to avoid name collision by using scoping for the interface and method dispatching, depending on whether we were using the interface type reference or the concrete-class type reference.




C# Class Design Handbook(c) Coding Effective Classes
C# Class Design Handbook: Coding Effective Classes
ISBN: 1590592573
EAN: 2147483647
Year: N/A
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net