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.

PHP

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';
        }
    }
}

XML

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.

Python

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.

.NET

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,

C++

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

namespace abc {
    int bar;
}
12341 questions
101
votes
32 answers

the name <...> does not exist in the namespace clr-namespace <...>

I have a small WPF application which used to compile just fine but is not anymore. I can't really say at which point it stopped building. It just worked fine one day, and the next it's not. Here's the project structure: There is no other projects…
ardal
  • 1,537
  • 3
  • 14
  • 18
100
votes
7 answers

Can't find System.Windows.Media namespace?

I'm using an object from a 3rd party API that has a property of type System.Windows.Media.ImageSource, yet I can't seem to find the System.Windows.Media namespace anywhere. If I try to add a reference to my project I don't see System.Windows.Media…
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
100
votes
5 answers

What is tempuri.org?

Why does tempuri.org exist? Why does each XML Webservice require its own namespace, unique from any other on the web?
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100
98
votes
1 answer

Splitting a Clojure namespace over multiple files

Is it possible to split a Clojure namespace over multiple source files when doing ahead-of-time compilation with :gen-class? How do (:main true) and (defn- ...) come into play?
Ralph
  • 31,584
  • 38
  • 145
  • 282
97
votes
3 answers

Ruby: what does :: prefix do?

I was reading through the source of Artifice and saw: module Artifice NET_HTTP = ::Net::HTTP # ... end line: https://github.com/wycats/artifice/blob/master/lib/artifice.rb#L6 Why not just do Net::HTTP instead of ::Net::HTTP, i.e., what does it…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
96
votes
1 answer

What the difference between a namespace and a module in F#?

I've just started learning F# (with little prior experience with .NET) so forgive me for what is probably a very simple question: What the difference between a namespace and a module in F#? Thanks Dave Edit: Thanks for the answer Brian. That's what…
Dave Berk
  • 1,591
  • 2
  • 15
  • 17
95
votes
3 answers

How can I get all classes within a namespace?

How can I get all classes within a namespace in C#?
Micheal sonnal
95
votes
9 answers

Namespace or Assembly?

I am getting very confused between Namespaces and Assemblies. Are System.Data and System.Web Namespaces or Assemblies? I have noticed these are called namespaces and at the same time they are present in GAC_32 folder. So what exactly are they?
user3125433
  • 973
  • 1
  • 8
  • 7
94
votes
11 answers

"Can't find Project or Library" for standard VBA functions

So I'm having to run someone else's excel app on my PC, and I'm getting "Can't find Project or Library" on standard functions such as date, format, hex, mid, etc. Some research indicates that if I prefix these functions with "VBA." as in "VBA.Date"…
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
94
votes
9 answers

How can I retrieve the namespace to a string C#

I am writing a program which needs the namespace of the program but I cant seem to figure out how to retrieve it. I would like the end result to be in a string. I was able to find an MSDN page about this topic but it proved to be unhelpful to…
Elliot Ames
  • 1,039
  • 1
  • 10
  • 8
94
votes
7 answers

How can I "unuse" a namespace?

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having... using namespace xyzzy ...statements in them, which impact on my code when I least want or expect it. Is there a way I…
Roddy
  • 66,617
  • 42
  • 165
  • 277
93
votes
2 answers

Creating a class within a function and access a function defined in the containing function's scope

Edit: See my full answer at the bottom of this question. tl;dr answer: Python has statically nested scopes. The static aspect can interact with the implicit variable declarations, yielding non-obvious results. (This can be especially surprising…
Gabriel Grant
  • 5,415
  • 2
  • 32
  • 40
90
votes
4 answers

What is the Greasemonkey namespace needed for?

I'm learning how to use Greasemonkey, and was wondering what the @namespace metadata id is for. Does it have to be a web address? Or can it be a folder/directory on my computer? Does it even need to be filled in?
Nope
  • 34,682
  • 42
  • 94
  • 119
88
votes
3 answers

How to forward declare a class which is in a namespace

I am trying to use forward declarations in header files to reduce the number of #include used and hence reduce dependencies when users include my header file. However, I am unable to forward declare where namespaces are used. See example…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
87
votes
20 answers

How to use jQuery for XML parsing with namespaces

I'm new to jQuery and would like to parse an XML document. I'm able to parse regular XML with the default namespaces but with XML such as:
Brian Liang
  • 7,734
  • 10
  • 57
  • 72