Reputation: 1758
I have created the following class:
using System.Windows;
using System.Windows.Input;
using MyUtils.MyArgParser;
namespace MyUtils.MyImplement
{
public static class ImplementExitOnEscape
{
#region ImplementExitOnEscape
public static void Implement(Window window)
{
window.KeyDown += Window_KeyDown;
}
private static void Window_KeyDown(object sender, KeyEventArgs e)
{
var window = sender as Window;
// Close window when pressing the escape key.
if (e.Key == Key.Escape) if (window != null) window.Close();
var optionX = MyArgParser.MyArgParser.GetOptionValue("optionX");
}
#endregion //ImplementExitOnEscape
}
}
Why am I forced to use the full name space for the MyArgParser
class in var optionX = MyArgParser.MyArgParser.GetOptionValue("optionX");
instead of just MyArgParser.GetOptionValue("optionX");
?
using MyUtils.MyArgParser;
gets ignored. having it there or not wouldn't make any difference, the compiler still forces me to use the full namespace.
I find this weird because it is not happening everywhere. For example, I am not required to use the full namespace in the file where the entry point of my application is defined.
Upvotes: 2
Views: 2202
Reputation: 61912
You have your using
directives outside your namespace
declaration. The compiler searches for a type or namespace in these locations, in this order:
MyUtils.MyImplement.ImplementExitOnEscape
(nested types inside the current type)MyUtils.MyImplement
(types or namespaces in current namespace)MyUtils
(types or namespaces in "next" outer level)System.Windows
, System.Windows.Input
, and MyUtils.MyArgParser
that you have using
s for (types or namespaces)You entered MyArgParser.
<something>.
In (3.) a match is found, and that is the namespace. Only in (5.) would your using
matter. So MyArgParser
refers to the namespace, not the type.
If you had put your using
s inside the namespace
block, things would have been different, see an answer of mine in another thread.
Upvotes: 1
Reputation: 14376
The problem is you have a class with the same name as its namespace, meaning the compiler cannot differentiate between MyArgParser in MyArgParser.GetOptionValue being a namespace or a class.
It may or may not force you to use the full namespace due different using statements at the top of each file or a field or variable whose name clashes with the class name. See Eric Lippert's Blog post (and parts 2, 3 and 4) on the subject for more information.
See How to avoid having the same name for a class and it's namespace, such as Technology.Technology? for more discussion on this.
Upvotes: 6
Reputation: 21
There could be few reasons:
Conflict with another name
Your class could be under yet another name.
Use Ctrl+J+K, and search for your class. See if VS finds it. If not, then it may be because of refercence issues. Do you need to add a reference to that library?
Upvotes: 0
Reputation: 15557
var optionX = MyArgParser.MyArgParser.GetOptionValue("optionX");
You class is named as your namespace, so to distinct between them you need to explicitly fully reference it.
To solve it, either change your MyArgParser namespace to (for example) MyArgParserNS and you can use it directly
using MyUtils.MyArgParserNS
And then:
var optionX = MyArgParser.GetOptionValue("optionX");
Or, well, fully reference it.
Upvotes: 7