Why lookup can't found base class?

  • Thread starter Thread starter Alex Sedow
  • Start date Start date
A

Alex Sedow

Why C# lookup mechanism can't found indirect base class by name?
It is seems very strange: derived class can access to base members, but not
class itself.
Example:

namespace N1
{
class B
{
public static int i;
}
class D : B
{}
}

class C : N1.D
{
B * b; // error! class indirectly derived from B, can't found it by name
void f()
{
int a = i; // OK, class can use members of class B
}
}

Alex.
 
You have to put a using directive into the file. If you are able to acess
members of a class doesn't mean that this class is really in scope.
 
Hi Alex,

You can access any class as long as it is not restricted by its access
modifier. As long as the classes are defined in the same file you can see
any class.

The problem i sthat the name of the class 'B' is not 'B' itslef.
It is N1.B.

C# compiler saves some typing by allowing you to use a short names, but you
have to use 'using' directive to tell it what namespaces to probe for that
class (If it probes on any namespace it will most likely end up with
ambiguity. How can it possibly know what 'B' you had in mind?). Or you can
define the class in the same namespace. The compiler cannot look up the
Look at this version of your example

namespace N1
{
class B
{
}
}
namespace N2
{
class B:N1.B
{
}

}

namespace N3
{
class D:N2.B
{
B b; //what on the earth is that 'B' is it N1.B or N2.B?. And they don't
even have to be in an iheritance relation
}
}
 
Back
Top