views:

153

answers:

3

The code I'm working with has a class called Environment that is not in any namespace. Unfortunately if I am in a class that imports the System namespace, there is no way to refer to the custom class called Environment. I know this was an unfortunate choice and should be refactored, but is there any way I can explicitly refer to the conflicting class?

In C++ it seems the way to do this is by using ::, and in Java there is something called global:: How do I do it in C#?

+12  A: 

C# also has a global (or unnamed) namespace - you can use global:: to access your class:

global::Environment 

See more on MSDN. Also see the :: operator.

You can create an alias for it as well:

using myEnv = global::Environment;
using sysEnv = System.Environment;
Oded
@Oded your answer is a bit irrelevant. Question was: "but is there any way I can explicitly refer to the conflicting class"
Andrey
@Andrey - The example shows exactly how to _explicitly_ refer to the `Environment` class that has no namespace.
Oded
@Oded sorry, i misread question
Andrey
Why didn't I try that... I knew about the Java solution :S
JoelFan
FYI you can also use a similar trick if you end up in the unfortunate situation of having two referenced DLLs which have the same type name in the same namespace. You say "extern alias FOO;" and then you can use "FOO::Blah.Bar" to mean "the Blah.Bar that appears in foo.dll". You just have to remember to say /r:FOO=foo.dll on the command line.
Eric Lippert
+1  A: 

Should be global::Environment just like in Java

Kristian Hebert
A: 

The code I'm working with has a class called Environment that is not in any namespace

You should absolutely change that. Or if it’s not your code, file a bug report and defer usage until the bug is fixed. Not using a namespace – that’s an absolute no-go.

(Notwithstanding the well-working solution posted by @Oded.)

Konrad Rudolph