I know this is probably simple, but I can’t seem to figure out if it’s possible to do this.
I have the following code:
public class A {
thisMethod();
public class B {
someMethod();
}
}
public class C {
A myA = new A();
A.B.someMethod();
}
Why can’t I access B if I’ve already instantiated A?
THanks for your help in advance!
You need an instance of A.B to call an instance method on A.B:
In your example you weren’t even trying to use the new instance you’d created.
If you come from a Java background, it may be worth pointing out that nested classes in C# are like static nested classes in Java. There’s no implicit reference from an instance of the nested class to an instance of the container class. (The access is also the other way round – in Java an outer class has access to its nested class’s private members; in C# the nested class has access to its outer class’s private members.)