C Sharp

Multiple Interfaces

Let me be clear on this because multiple inheritance has become a controversial subject on many newsgroups and mailing lists: C# does not support multiple inheritance through derivation. You can, however, aggregate the behavioral characteristics of multiple programmatic entities by implementing multiple interfaces. Interfaces and how to work with them will be covered in Chapter 9, "Interfaces." For now, think of C# interfacesas you would a COM interface.

Having said that, the following program is invalid: -

class Foo
{
}
class Bar
{
}
class MITest : Foo, Bar
{
    public static void Main ()
    {
    }
}

The error that you'll get in this example has to do with how you implement interfaces. The interfaces you choose to implement are listed after the class's base class. Therefore, in this example, the C# compiler thinks that Bar should be an interface type. That's why the C# compiler will give you the following error message: -

'Bar' : type in interface list is not an interface

The following, more realistic, example is perfectly valid because the MyFancyGrid class is derived from Control and implements the ISerializable and IDataBound interfaces: -

class Control
{
}
interface ISerializable
{
}
interface IDataBound
{
}
class MyFancyGrid : Control, ISerializable, IDataBound
{
}

The point here is that the only way you can implement something like multiple inheritance in C# is through the use of interfaces.

by BrainBellupdated
Advertisement: