Namespaces and inheritance

  • Thread starter Thread starter Karen
  • Start date Start date
K

Karen

Hi

I was just wondering - when you you use the using feature in C# to
reference a namespace in your project - does it enable you to write
classes that inherit from classes in the referenced namespace?

Thanks.
 
Yes.
Either base class exists in referenced namespace or fully qualify namespace
before base class.
 
The using statement is just to set up a shortcut so that you don't
necessarily have to type the fully qualified name for classes in your
code.

So, if you have class Test1 in namespace N1, you could do either of
these:

N1.Test1 a = new N1.Test1();
or
using N1
Test1 a = new Test1();

This assumes you have added a reference to the assembly defining
Namespace N1 to your project.
Sometimes you still have to use fully qualified names if you have name
collisions (e.g.:
using N1;
using N2;
//assume there is a Test1 in N1 and a different Test1 in N2, then you
still have to write
N1.Test1 a = new N1.Test1();

Now, back to your question,
the using statement does not impact the way you can write classes and
inherit classes, as far as I know.

So, assuming you have a class BaseClass in a namespace N1, and that you
have added a reference to the N1 assembly in your project, you can
'write' about that class either with or without the using statement:
without it:
N1.BaseClass b = new N1.BaseClass();
with it:
using N1;
BaseClass b = new BaseClass();

as far as inheriting from BaseClass, again, the using statement doesn't
affect the behavior. You may not be able to inherit from the base class
if it is sealed, but otherwise, you can do either of the following:

without using:
public class MySample : N1.BaseClass {...}
with using stmt:
using N1;
public class MySample : BaseClass {...}

Again, this assume you have added a reference to the N1 assembly in
your project.

HTH,
and I hope I'm not mistaken.

F.O.R.
 
Back
Top