Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assume we have the following code in C# source code file file1.cs. According to C# specs because class Class1 is not explicitly defined within a user-defined namespace (named namespace) it is instead defined within the "unnamed namespace":

public class Class1
{
    //body
}



In the bottom example I define a user-defined namespace SampleNamespace. Say defined in C# source code file file2.cs. In a different source code file from which class Class1 is found in. My question is: Is namespace SampleNamespace also defined within the "unnamed namespace"? Just as how class Class1 is defined in the unnamed namespace?

namespace SampleNamespace
{
    public class Class2
    {
        //body
    }
}


What I have tried:

Microsoft, C# langauge, Visual Studio, Langauge specs
Posted
Updated 26-Aug-21 14:32pm
v2

It seems you have discovered the "Global" NameSpace: I would call it "root," or, "default," but, "unnamed" makes no sense

There are aspects of this NameSpace that you may find unexpected. Few programmers work directly with the Global NameSpace: using it to hold application-wide variables and data is considered poor practice, a violation of SOLID principles.

For sample usages, see: [^]

I can't describe the complexity of the Global NameSpace better than this :) c# - What is global::? - Stack Overflow[^]
 
Share this answer
 
Comments
MShanoda 26-Aug-21 20:58pm    
Hi there, I concur that "unnamed namespace" is not the best term to use. However I use that term as it was the C# language spec uses as well.

According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/namespace : "The compiler adds a default namespace. This unnamed namespace, sometimes referred to as the global namespace, is present in every file."

Concerning the question in my post though, the other user who answered my question claimed that (root) user-defined namespace are not part of the unnamed namespace.

In contradiction to his claim, I found an MS documentation that claims otherwise: "Namespaces and types that have no enclosing namespace are members of the global namespace. This corresponds directly to the names declared in the global declaration space. Namespaces and types declared within a namespace are members of that namespace. This corresponds directly to the names declared in the declaration space of the namespace."

Source: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#:~:text=Hiding%20through%20inheritance).-,Namespace%20members,-Namespaces%20and%20types

What do you make of the above statement. It clearly states that (root) user-defined namespaces are defined in the unnamed namespace. Right?
BillWoodruff 27-Aug-21 3:25am    
If you have issues with what Dave said, take that up with him.

Are you here to learn to be a better programmer, and, to appreciate the people who volunteer to assist you, or, are you here to engage in a semantic debate which is pointless ?
No, it doesn't. Your's is just another "root" namespace, just like System or Microsoft.

You can create other child namespaces just by including your root namespace before the child.
C#
namespace SampleNamespace
{
    public Class1
    {
    }
}

C#
namespace SampleNamespace.ChildNamespace
{
    public SomeClass
    {
    }
}
 
Share this answer
 
Comments
MShanoda 26-Aug-21 18:24pm    
Your solution adds on to the point I was making.

To paraphrase, isn't SampleNamespace also child of the (root) unnamed namespace? similar to how ChildNamespace is a child to the SampleNamespace.
Dave Kreskowiak 26-Aug-21 18:26pm    
There is no unnamed namespace above SampleNamespace. It's just another "root" namespace:
System
Microsoft
SampleNamespace
unnamed
MShanoda 26-Aug-21 18:33pm    
Then are you saying that the unnamed namespace is only a parent to "types" not defined in a user-defined namespace? And so does not apply any (root) user-defined namespace that I define?
Dave Kreskowiak 26-Aug-21 18:52pm    
Yes, but "does not apply" has no definition so I don't know what you mean by that.
MShanoda 26-Aug-21 19:02pm    
I meant if you were saying the same (the unnamed namespace is a parent to ***) does not apply to user-defined (root) namespaces in a source code file. Which based on conversation. I pressume yes.

To add on to that, I haven't read any online source that states what you are claiming. Is there any way to test the claim you are making in code?

That the unnamed namespace is not a parent to any (root) user-defined namespace I define.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900