In the code below, I want to access StackElement.below
in LinkStack.Pop()
, so I cannot decorate StackElement.below
as private. However I also don't want to make it public because it's not safe if any other class can access the property. What should I do then? Thanks in advance.
public class StackElement
{
public int value;
public StackElement below;
}
public class LinkStack
{
private StackElement topElement;
private int count, capacity;
void Pop()
{
topElement = topElement.below;
}
}
CodePudding user response:
If you're trying to restrict the below
field to only the StackElement
and the LinkStack
class, then it's best to put the LinkStack
class inside StackElement
.
public class StackElement
{
public int value;
private StackElement below; // now private
public class LinkStack
{
private StackElement topElement;
private int count, capacity;
void Pop()
{
topElement = topElement.below;
}
}
}
I don't know if that is architecturally appropriate for your application or not.
If you only need to protect the field from access outside the assembly, then make it internal
instead.
internal StackElement below;