Questions tagged [namespaces]

A namespace is a container that provides context for identifiers, within which names are unique.

A namespace is a container that provides context for identifiers, within which names are unique. In many implementations, identifiers can be disambiguated between namespaces by prepending the identifier with the namespace, separated by a delimiter such as a period (.) in and , double-colon (::) in or backslash (\) in .

For many programming languages, namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory, but one file may have the same name multiple times.

As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace. A namespace is also called a context, because the same name in different namespaces can have different meanings, each one appropriate for its namespace.

Following are other characteristics of namespaces:

  • Names in the namespace can represent objects as well as concepts, be the namespace a natural or ethnic language, a constructed language, the technical terminology of a profession, a dialect, a sociolect, or an artificial language (e.g., a programming language).
  • In the Java programming language, identifiers that appear in namespaces have a short (local) name and a unique long "qualified" name for use outside the namespace.
  • Some compilers (for languages such as C++) combine namespaces and names for internal use in the compiler in a process called name mangling.


Namespaces were introduced into PHP from version 5.3 onwards. In PHP, a namespace is defined with a namespace block.

namespace phpstar {
    class fooBar {
        public function foo() {
            echo 'hello world, from function foo';

        public function bar() {
            echo 'hello world, from function bar';


In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.


In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical as well. In general when a module is imported then the names defined in the module are defined via that module's name space, and are accessed in from the calling modules by using the fully qualified name.


All .NET Framework classes are organized in namespaces. When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name,


In C++, a namespace is defined with a namespace block.

namespace abc {
    int bar;
12341 questions
46 answers

What does if __name__ == "__main__": do?

What does this do, and why should one include the if statement? if __name__ == "__main__": print("Hello, World!") If you are trying to close a question where someone should be using this idiom and isn't, consider closing as a duplicate of Why…
  • 177,705
  • 43
  • 90
  • 110
42 answers

Why is "using namespace std;" considered bad practice?

I have heard using namespace std; is bad practice, and that I should use std::cout and std::cin directly instead. Why is this? Does it risk declaring variables that share the same name as something in the std namespace?
  • 34,001
  • 6
  • 25
  • 36
15 answers

Should 'using' directives be inside or outside the namespace in C#?

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside instead of outside the namespace?
  • 37,735
  • 14
  • 62
  • 96
10 answers

What does __all__ mean in Python?

I see __all__ in files. What does it do?
  • 16,849
  • 3
  • 20
  • 19
29 answers

How do I declare a namespace in JavaScript?

How do I create a namespace in JavaScript so that my objects and functions aren't overwritten by other same-named objects and functions? I've used the following: if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();} Is there a more…
Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
11 answers

Unnamed/anonymous namespaces vs. static functions

A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so: namespace { int cannotAccessOutsideThisFile() { ... } } // namespace You would think that such a feature would be useless -- since you can't specify the name of…
Head Geek
  • 38,128
  • 22
  • 77
  • 87
9 answers

What is the meaning of prepended double colon "::"?

I found this line of a code in a class which I have to modify: ::Configuration * tmpCo = m_configurationDB;//pointer to current db and I don't know what exactly means the double colon prepended to the class name. Without that I would read:…
  • 6,241
  • 6
  • 26
  • 27
7 answers

How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

I'm trying to use the new bundling feature in a project I recently converted from MVC 3 to MVC 4 beta. It requires a line of code in global.asax, BundleTable.Bundles.RegisterTemplateBundles();, which requires using System.Web.Optimization; at the…
Ian Davis
  • 19,091
  • 30
  • 85
  • 133
6 answers

What are inline namespaces for?

C++11 allows inline namespaces, all members of which are also automatically in the enclosing namespace. I cannot think of any useful application of this -- can somebody please give a brief, succinct example of a situation where an inline namespace…
  • 44,150
  • 20
  • 113
  • 196
5 answers

Get type name without full namespace

I have the following code: return "[Inserted new " + typeof(T).ToString() + "]"; But typeof(T).ToString() returns the full name including namespace Is there anyway to just get the class name (without any namespace qualifiers?)
  • 188,729
  • 360
  • 878
  • 1,366
9 answers

Namespace + functions versus static methods on a class

Let's say I have, or am going to write, a set of related functions. Let's say they're math-related. Organizationally, should I: Write these functions and put them in my MyMath namespace and refer to them via MyMath::XYZ() Create a class called…
  • 4,106
  • 4
  • 20
  • 7
6 answers

Why are unnamed namespaces used and what are their benefits?

I just joined a new C++ software project and I'm trying to understand the design. The project makes frequent use of unnamed namespaces. For example, something like this may occur in a class definition file: // namespace { const…
Scottie T
  • 11,729
  • 10
  • 45
  • 59
47 answers

How do I fix a "type or namespace name could not be found" error in Visual Studio?

I'm getting a: type or namespace name could not be found error for a C# WPF app in VS2010. This area of code was compiling fine, but suddenly I'm getting this error. I've tried removing the Project Reference and the using statement, shutting…
  • 34,042
  • 79
  • 253
  • 454
11 answers

Getting all types in a namespace via reflection

How do you get all the classes in a namespace through reflection in C#?
6 answers

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian…
2 3
99 100