Search code examples
c#access-modifiers

Why can I call a private method of another instance of the same type outside of that instance?


If I have ObjectA, and it has a private method GetPrice() and also has a "parent" field of the same type, why am I able to call GetPrice() on the parent instance from within the child instance?

Example:

private decimal GetPrice()
{
    ObjectA parent = Parent;

    if(parent != null)
    {
        return parent.GetPrice(); // Why is this OK?
    }

    return 0;
}

Solution

  • Because private means "not accessible to other types", not "not accessible to other instances".