0

I feel like this is a silly question, but I can;t seem to figure out why this is happening. Consider the following:

// file: A.cs
class A {
    public static string ns = "global";
}


// file: myNamespace\A.cs
namespace myNamespace {
    class A {
        public static string ns = "myNamespace";
    }
}


// in some other file...
using myNamespace;

class myClass {
    public void myMethod() {
        Console.WriteLine(A.ns);
    }
}

// OUTPUT: global

Now, since I have declared I want to use myNamespace; why is the compiler stubbornly trying to use the global version instead of the namespaced version? What is the point of using if it is going to do this?

My real code is more complicated than this of course. In the real code I am having the issue in (Unity API), I am getting a compile error in a case more like this:

A myObj = gameObject.GetComponent<A>();

This gives me an argument exception. Not sure if maybe this has to do with generics? Am I missing something very dumb?

dmarra
  • 853
  • 8
  • 23
  • 1
    If you want the namespace version, use `myNamespace.A`. The compiler searches for the type starting in the global namespace, it stops when it finds it, how is it supposed to know if you mean the one in the global or the specific? Just including `using` doesn't create a rule that excludes types in more accessible namespaces. – Ron Beyer Nov 09 '15 at 20:03
  • I see. I suppose that makes sense now that I think of it. In reality, the class it chooses is deprecated in my codebase anyways, so I suppose my best bet is to remove it from the global namespace. Thanks for the clarification! – dmarra Nov 09 '15 at 20:12
  • Yes, see [my answer in another thread](http://stackoverflow.com/a/16092975/1336654). – Jeppe Stig Nielsen Nov 09 '15 at 22:39

1 Answers1

1

I can't test it or find the spec right now, but I believe the resolution rules will search the namespace that the calling code is in first. Since your calling code is in the global namespace, that namespace is searched fist. If you put the calling code inside the myNamespace namespace I suspect you'd get the behavior you are looking for.

If the calling code was not in either namespace then you should get an "ambiguous type" compiler error.

Also note that your classes are internal by default, although if your classes are all in the same assembly that should be an issue.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I got a different error, but that is only because the exception is caught by GetCompnent<>() and a new one thrown. But yes, my calling code is in a different namespace altogether. I think I know what I need to do. thanks! – dmarra Nov 09 '15 at 20:13
  • I think the relevant section of the spec is 10.8, but its written in a way that's difficult to follow (in my lack of sleep today at least). – Ron Beyer Nov 09 '15 at 20:16
  • It will not be "ambiguous" between a type in a named namespace and a type in the global namespace. You are right the "current" namespace is searched first. If the `using` directive stands on the global namespace level, the global namespace is searched before the namespace from the `using` directive. If the `using` stands inside some `namespace` declaration, that `using` is considered before we "move out" of the current `namespace`. But see my link in my new comment to the question. – Jeppe Stig Nielsen Nov 09 '15 at 22:48