Namespace

from Wikipedia, the free encyclopedia

The namespace ( English namespace ) is a term used in programming .  The names for objects are arranged in a kind of tree structure - especially with object-oriented programming - and are clearly addressed using appropriate path names .

In simple terms, this means that each name uniquely identifies an object within such a space . However, the same name can be freely used in another namespace to designate another object. In addition, these independent namespaces can be linked within a hierarchy.

Technical details

A name identifies an object . For a clear assignment, however, the corresponding context - the namespace  - must be observed. The description is usually done in the programming by the so-called "dot notation", whereby the individual objects, with their properties ( attributes ) and methods , are addressed analogously to a tree structure . In addition to dot notation, other characters are also used, such as B. for file names with slashes (" ") or backslashes (" "). Some namespaces, e.g. B. file systems are structured hierarchically, i. that is, they can themselves consist of namespaces. Namespaces are used to prevent conflicts when assigning names. Graphically, namespaces are equivalent to trees ; that is, they have a root (a fixed, defined starting point), nodes (directories) and leaves (objects). /\

The idea of ​​namespaces is also used in other areas under different names, e.g. B. in telephony . Each participant receives an individual phone number , e.g. B. 4711, and this is assigned locally. The telephone network is subdivided into sub-networks and identification takes place via the area code . This means that each number can be assigned multiple times; it only needs to be unique within the subnet. When calling in the same area code, it is sufficient to specify the number 4711. If a subscriber from the area code 0815 who also has the number 4711 is to be contacted, 0815 is dialed. With this technology, several participants can have the same call number 4711. In this example, 0815 would be the namespace, 4711 the actual name and the target telephone connection the identified object.

When creating programs , an author can use namespaces to write large program packages with many defined names without having to worry about whether the newly introduced names conflict with other names. In contrast to the situation without namespaces, not the whole name is introduced here, but only part of the name, namely that of the namespace.

A namespace is a declaratory area that attaches an additional identifier to each name that has been declared in it. This additional identifier makes it less likely that a name conflict will occur with names declared elsewhere in the program . It is possible to use the same name in different namespaces without conflict, even if the same name occurs in the same translation unit. As long as it appears in different namespaces, each name is unique due to the added namespace identifier.

Most modern programming languages support namespaces. The markup language XML also supports namespaces, whereby the prefix is ​​separated from the local name by a colon .

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

As a rule, names in a namespace cannot have more than one meaning. That is, different meanings cannot have the same name in the same namespace. A namespace is also referred to as a context because the same name can have different meanings in different namespaces, each of which is suitable for its namespace.

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 .

In addition to the technical use of the abstract language described above, some programming languages ​​have, among other things, a certain keyword that is used for the explicit control of namespaces.

Examples

C ++

namespace Germany
{
    string anthemTitle = "Das Lied der Deutschen";
}

namespace Austria
{
    string anthemTitle = "Land der Berge";
}

void output1()
{
    string anthemTitle = "Schweizerpsalm";
    cout << Germany::anthemTitle << endl;  // Ausgabe: Das Lied der Deutschen
    cout << Austria::anthemTitle << endl;  // Ausgabe: Land der Berge
    cout << anthemTitle << endl;  // Ausgabe: Schweizerpsalm
}

void output2()
{
    using namespace Germany;

    cout << anthemTitle << endl;  // Ausgabe: Das Lied der Deutschen
    cout << Austria::anthemTitle << endl;  // Ausgabe: Land der Berge
}

void output3()
{
    using namespace Austria;

    cout << Germany::anthemTitle << endl;  // Ausgabe: Das Lied der Deutschen
    cout << anthemTitle << endl;  // Ausgabe: Land der Berge
}

void output4()
{
    using namespace Germany;
    using namespace Austria;

    cout << anthemTitle << endl;  // Fehler: Referenz auf anthemTitle ist mehrdeutig
}

Namespaces can also be nested in C ++:

namespace nested1
{
    namespace nested2
    {
        namespace nested3 { /* ... */ }
    }
}

C #

Namespaces are often used in the C # programming language . All classes of the .NET Framework are organized in namespaces. In addition, custom namespaces are often used by programmers to organize their work and avoid name collisions. When referring to a class, you should either include its fully qualified name, that is, the namespace followed by the class name, or add a using statement. This eliminates the need to specify the full name of all classes in this namespace. In the following example, System is a namespace and Console and Convert are classes that are defined there.

namespace Space
{
	class Program
	{
		public static void Main(string[] args)
		{
            Console.WriteLine("Hallo Europa! Hallo Amerika!");
            int number = Convert.ToInt32("3");
            Console.WriteLine("Hallo " + number + ". Welt!");
		}
	}
}

PHP

namespace Fahrzeug\PKW;
use Antrieb\Motor;

class Kleinwagen {

    protected $motor;

    public function __construct(Motor $motor) {
        $this->motor = $motor;
    }

}

Public namespaces

In the case of public namespaces, there is a particular problem of administration, because all (market) participants have to agree so that no two names are differentdenotes things. For this reason, there are usually administrative organizations that manage namespaces or parts of them and reserve sections of them for individual participants. Here again there is the problem that these administrative organizations usually have a monopoly position themselves, so that if they are profit-oriented they have monopoly pricesse could achieve to the disadvantage of the participants. In addition, such a monopoly represents a weak point, because if the administrative organization fails, business operations can, depending on the integration of the participants, dThis participant will be significantly disturbed. This not only applies, but also to the Domain Name System , because in this case the name resolution must be done onlinehen.

See also

Web links

Wiktionary: namespace  - explanations of meanings, word origins, synonyms, translations