By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

Inherit more than on class

P: n/a
(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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.