Fog Creek Software
Discussion Board




A lame question: understanding namespaces


Sorry if my question sounds lame. I have recently found myself baffled by the way namespaces are laid out in .NET. Or I must concede I have not read enough about them, and am new to them.

We have the System namespace, for instance. The namespace has a class, say, String. This would be refered to as:

System.String

right?

There's another namespace, for instance, that comes to mind - the System.Data.ADO namespace, if I recall correctly. This namespace contains ADO.NET objects like the ADOConnection, ADOCommand etc, right?

My questions are:

(1) Is it possible for a namespace name to have a period/dot in it? As I guess, the answer is yes. If it is so, then would the System.Data.ADO namespace have been declared thusly:

Namespace System
{
    Namespace Data
    {
        Namespace ADO
        {
              //Member classes, data and methods
        }
    }
}

or as such?:

Namespace System.Data.ADO
{
    //Member classes, data and methods
}


If you say both are legal declarations, then would it not create a confusion if we had two namespaces declared as the cases presented above? There would be a scope conflict if the members contained in the namespaces had common names, and that would in affect defeat the purpose of namespaces.

(2) Secondly, if you do have a namespace called Tree.Branch.Leaf, and if both the declarations mentioned in point 1 above were legal, then, just by looking at some code that said

Tree.Branch.Leaf.Stomata objStomata = new Tree.Branch.Leaf.Stomata();

How would one know whether the namespace is laid thus:

Namespace Tree
    Namespace Branch
    {
        Namespace Leaf
        {
            //Member classes, props and methods

        } //End of namespace Leaf
    } //End of namespace Branch
} // End of namespace Tree

or whether there is a single namespace element called Tree.Branch.Leaf?

PS: I've read the articles on the MSDN website about namespaces.

Sathyaish Chakravarthy
Tuesday, June 15, 2004

The following declarations are exactly equivalent:

namespace A.B.C { ... }

namespace A {
    namespace B {
        namespace C { ... }
    }
}

The first version is simply an abbreviation for the second. Why do you think there would be any ambiguity?

Chris Nahr
Tuesday, June 15, 2004


The confusion stems from the fact that the documentation reads, "A namespace name may have a period." That led me to think that

A.B.C was a distinct namespace irrespective of the fact that there did not exist individual namespaces B and C that were nested in A.

Thanks for the clarification! I have no doubts on this one now.

Sathyaish Chakravarthy
Tuesday, June 15, 2004

Ah, I see. The documentation is indeed misleading -- the name of an _individual_ namespace cannot have a period because the period is used to separate nested namespaces. The _combined_ namespace name can therefore have one or more periods but those periods always indicate nesting levels.

Chris Nahr
Tuesday, June 15, 2004

I think the problem is in .NET, like Java, the period is overloaded for scoping rules.  C++ has :: .  -> but probably shouldn't have overloaded :: to scope classes and namespaces.

christopher baus (www.baus.net)
Tuesday, June 15, 2004

*  Recent Topics

*  Fog Creek Home