Inherit more than on class

S

Slavyan

(I just started to learn C#.NET)

What's the syntax in c# for a class to inherit more than one class.
I know following syntax:
public class MyClass : MyOtherClass
{
}


but I need to inherit one more class.
Something like this won't work:
public class MyClass : MyOtherClass : MyAnotherClass
{
}

neither does this
public class MyClass : MyOtherClass, MyAnotherClass
{
}

so could someone give me a hint.

And by the way does anyone know any good resources on internet to learn C#.

Thanks.
 
I

Ignacio Machin

Hi,

In C# you cannot inherit from mroe than one class. You can inherit from one
and implement several interfaces, though.

Hope this help,
 
T

Tu-Thach

You can only inherit from ONE class, but you can implement
many interfaces. The syntax is

public class MyClass : MyBaseClass, IMyInterface
{
}

Tu-Thach
 
J

Jon Skeet

Slavyan said:
(I just started to learn C#.NET)

What's the syntax in c# for a class to inherit more than one class.

You can't. .NET has single inheritence of implementation, but multiple
inheritence of interface. In other words, you can do:

// Inherit from one class, and implement two interfaces
public class Foo : Stream, IDisposable, IComparable
{
....
}

but you can't do

public class Foo : StreamReader, XslTransformer
{
....
}
 
C

Chris Hornberger

As has been pointed out, you can only inherit from one class at a
time, but there is no real limit to the number of linear inherits you
can perform. So, if you need a blue 4-door convertible, you can:

class vehicle {...}

class car : vehicle {...}

class 4doorCar : car {...}

class 4DoorConvertible : 4doorCar {...}

class blue4DoorConvertible : 4doorConvertible {...}

Forgive my overly simplistic example, but you get the point. People
will pick nits about most of that example inheritence chain should
really be properties of Car, but again... you get the point.

If you *REALLY* need multiple inheritence, you need to use C++, but it
can SERIOUSLY kick you in the rear end. The rule of thumb in multiple
inheritence is NEVER inherit from similar classes.

For instance "Thing" can inherit from Tomatoe and Buick, but you
wouldn't want to inherit from Tomatoe and Cucumber. The idea is you
want to keep any overlaps in functionality or interfaces to an
absolute minimum, and completely eliminate them where possible. Then
you have a whole issue with precedence in virtual function
dispatching, etc. It's messy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top