Is there a way in C# (4+) for the instance of class A (instanceOfClassA) in class B to access the variable x (as in SomeMethodInClassA) without x being public (see code example below)?
class A
{
B linkToClassB;
void SomeMethodInClassA()
{
linkToClassB.x = 1;
}
};
class B
{
public int x; // This should be accessible from class A without being public
A instanceOfClassA;
...
};
I was wondering that because as instanceOfClassA is a member of of class B there might be a way to allow a class to access pecific methods of their member classes.
There are two options for
Abeing able to accessxwithout the classes being part of the same hierarchy:Nested class
If
Ais a nested class insideBthenAcan accessx:internalaccessIf the two classes are in the same assembly and
xis beinternalthen it is possible forAto access it. Of course in this case it is possible to accessxfrom all other classes in the same assembly as well, which might not be desirable.