Resolving ambiguous references

K

Kyle Blaney

Suppose I have the following in file1.cs (all code is compacted to
reduce the number of lines):

namespace B {
namespace C {
class D {
public static void Print() {
System.Console.WriteLine( "B.C.D.Print" );
}
}
}
namespace E {
class D {
public static void Print() {
System.Console.WriteLine( "B.D.E.Print" );
}
}
}
}

Suppose I have the following in file2.cs:

using B.C;
using B.E;

namespace A {
class A1
{
public void Main()
{
D.Print();
}
}
}

As expected, the compiler complains about the call to D.Print in
file2.cs with the error "D is an ambiguous reference". I can resolve
the reference by changing the method call to "B.C.D.Print()" or
"B.D.E.Print()". I want to resolve the reference by adding a "using
B" directive and changing the method call to "C.D.Print()". However,
when I try that I get the error "The type or namespace name 'C' could
not be found."

Why can't I resolve the ambiguity the way I want?
 
A

Alexander Monakhov

Hello!

Using namesapce directive gives you access only to types immediately in
"used"( imported ) namespace. Compiler doesn't see 'C', 'cause it's a nested
namespace.

Best regards!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Multiple Namespaces in Separate File 2
Error CS0246 1
Static vs Non-static 8
inheritance problem 2
question on namespaces 7
LinkedListLibrary 2
where T : class 1
error :( 4

Top