explicitly refer to a class without a namespace in C#
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 开发者_如何学Ccustom 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#?
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;
Should be global::Environment
just like in Java
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.)
精彩评论