Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace conflict in C#

Tags:

namespaces

c#

There is a System namespace inside my program's namespace. And as a result I can't see the standard System namespace from within mine. How can I resolve this problem?

enter image description here

For example in C++ there is the :: operator which 'shifts' me out of my namespace, so I can see external namespaces with the same name as my current namespace:

enter image description here

Is there a similar operator in C#?

like image 556
Anton Semenov Avatar asked Apr 15 '11 19:04

Anton Semenov


People also ask

What is the namespace in C?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is namespace issue?

Namespace collision occurs if parts of the namespace delivered by different people have the same names. For example, two vendors might come up with the same library name and install in the same directory.

What is a namespace example?

Prominent examples for namespaces include file systems, which assign names to files. Some programming languages organize their variables and subroutines in namespaces. Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files.

Does C language support namespaces?

Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it.


2 Answers

You need to use the global keyword. That forces the namespace resolution to start at the very top. It's mostly used in generated code to be doubly sure the right namespace is referenced.

 global::System.Foo.Bar; 

Some MSDN documentation on it: http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

like image 154
Matt Greer Avatar answered Sep 21 '22 18:09

Matt Greer


For more convenience, you can give it an alias, too:

using GSystem = global::System; 

Will allow you to refer to the global System namespace as GSystem or whatever else you would like to call it.

like image 34
Ry- Avatar answered Sep 21 '22 18:09

Ry-