Namespaces and Inheritance

R

Robert Cramer

Is it perfectly acceptable, from a "conventions" or "best practices" point
of view to have a class in one namespace extend a class in another
namespace?

I just came across this state of affairs in the .NET Framework itself and,
at first, it seemed somehow like a "bad thing" (in part because namespaces
are intended to group classes that serve some common purpose) - but after
about two seconds of thinking about it, I'm thinking that there is
absolutely nothing wrong with doing this - and in many situations, like in
large frameworks, it would even be unavoidable.

And example from the .NET Frameowork is:
System.ComponentModel.ComponentResourceManager
which extends
System.Resources.ResourceManager

Thoughts? Opinions? - specifically regarding the idea that it is (or is not)
perfectly acceptable to have a base class in one namespace, and then extend
that class in an entirely different namespace?

FWIW, I do understand that the classes, themselves, don't care about the
namespace in which they exist - at least not beyond qualifying references to
specific external classes.

Thanks.
 
R

Roger Frost

Robert Cramer said:
Is it perfectly acceptable, from a "conventions" or "best practices" point
of view to have a class in one namespace extend a class in another
namespace?

I just came across this state of affairs in the .NET Framework itself and,
at first, it seemed somehow like a "bad thing" (in part because namespaces
are intended to group classes that serve some common purpose) - but after
about two seconds of thinking about it, I'm thinking that there is
absolutely nothing wrong with doing this - and in many situations, like in
large frameworks, it would even be unavoidable.

And example from the .NET Frameowork is:
System.ComponentModel.ComponentResourceManager
which extends
System.Resources.ResourceManager

Thoughts? Opinions? - specifically regarding the idea that it is (or is
not) perfectly acceptable to have a base class in one namespace, and then
extend that class in an entirely different namespace?

FWIW, I do understand that the classes, themselves, don't care about the
namespace in which they exist - at least not beyond qualifying references
to specific external classes.

Thanks.


If the extended class adds functionality specific to its namespace, why not
put it there?
 
P

Peter Duniho

Is it perfectly acceptable, from a "conventions" or "best practices"
point
of view to have a class in one namespace extend a class in another
namespace?

I think it depends on your definition of "extend". But for common
definitions, such as "inherit" or (now with C# 3.0) writing an extension
method for the class, I'd say that not only is it acceptable, it's the
most common way of doing it.

Every .NET Forms application has at least one custom Form-derived class,
and that class is never in System.Windows.Forms.

Similar situations may well exist within a development team. I don't see
anything fundamentally wrong with this kind of extension crossing
namespace boundaries.

Pete
 

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