views:

227

answers:

5

In C#, how many classes can you inherit from?

When you write:

using System.Web.UI;

is that considered inheriting from a class?

+7  A: 

A using directive:

using X.Y.Z;

only means that if an identifier isn't in the local namespace, the namespace X.Y.Z will be searched for that identifier as well. This allows you to have somewhat terser code, so you don't have to do System.IO.Path.Combine() if you have using System.IO.Path, for instance. I don't believe there is a limit on how many using statements you can have.

C# does not allow multiple inheritance -- you can only inherit from one class. However, you can implement as many interfaces as you'd like.

Mark Rushakoff
A using namespace directive can only be applied to namespaces; 'System.IO.Path' is a type not a namespace
najmeddine
+3  A: 

You can inherit from only one class.

Writing 'using System.Web.UI' has nothing to do with inheritance. This statement just means that you want to 'use' the classes that are in the namespace System.Web.UI, so that you can directly write the class-name, instead of having it to prefix with the namespace-name.

Frederik Gheysels
+7  A: 

No,

A using directive allows you to not have to type the fully qualified namespace when you add a control or a class to your code.

For instance, if I add:

using System.Web.UI;

Then I can simply write:

Page.Something

Instead of:

System.Web.UI.Page.Something

Inheritance
You can only inherit from one class, but you can implement as many Interfaces as you want.

If all of my pages inherit from a base class, then that base class will need to inherit from the Page class for it to do any good.

George Stocker
+1  A: 

No.

  1. using makes externally defined classes visible
  2. class Cat : Animal defines a class Pet that derives (inherits) from Animal

C#, unlike C++, does not support multiple inheritance, therefore you can only derive it from one class (However, you can implement any number of interfaces)

Jen
+1  A: 

I'd like to add an addendum to the answer given by Jen. It's correct to say that C# doesn't support multiple inheritance, but it can be useful to know that you can implement multiple inheritance in .NET by ignoring the CLS (the Command Language Subsystem). It's nasty work, and involves you rolling your own vtables, but it is possible.

Pete OHanlon