469,360 Members | 1,775 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Inherit more than on class

(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.
Nov 15 '05 #1
4 27161
Hi,

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

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Slavyan" <sl*****@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
(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.

Nov 15 '05 #2
Slavyan <sl*****@hotmail.com> wrote:
(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
{
....
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
You can only inherit from ONE class, but you can implement
many interfaces. The syntax is

public class MyClass : MyBaseClass, IMyInterface
{
}

Tu-Thach

-----Original Message-----
(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.
.

Nov 15 '05 #4
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.
"Slavyan" <sl*****@hotmail.com> wrote in message news:<e2**************@TK2MSFTNGP11.phx.gbl>...
(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.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sebastian Dau | last post: by
6 posts views Thread by mister.mwa | last post: by
3 posts views Thread by cagenix | last post: by
2 posts views Thread by aia | last post: by
11 posts views Thread by Nathan Laff | last post: by
11 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.