Common Type System—Private Inheritance Accessibility


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Private Inheritance Accessibility

One hidden problem when using private interface inheritance is the fact that the implemented methods are generated as private. They can be accessed through the interface map, but it does mean that subclasses will have no way to access the methods. For example, reimplementing an interface and "chaining" to the base implementation is a common practice. But with private implementations, you cannot do this.

Consider if we had a PrivateExtender type that wanted to redefine Foo, but still make use of the base type’s version:

class PrivateExtender : PrivateImplementer, IFoo
{
   void IFoo.Foo()
   {
      base.Foo(); // This line fails to compile
      Console.WriteLine(“PrivateExtender::IFoo.Foo);
   }
}

We’d normally accomplish this by making a call through the base keyword in C#. But this won’t compile, because Foo is private on PrivateImplementer. The syntax you might imagine for this situation could look like ((IFoo)base).Foo(), but alas that doesn’t compile (and indeed there isn’t any representation for it in IL). You could write ((IFoo)this).Foo(), but that’s just going to get you into an infinite loop (since it calls virtually to PrivateExtender’s copy—the same method doing the call). You just can’t write the code to do it!


Previous_Page_.gif Next_Page_.gif





Personal tools
Share this page