locked
Within a class, calling a private method of the same class RRS feed

  • Question

  • Hi,

    I'm aware that C# allows you to call private methods of a different instance of a class provided you do it within that class, for example:

    public class Foo
    {
            private void DoSomethingPrivate()
            {
            }
    
            public void DoSomethingPublic(Foo differentInstance)
            {
                differentInstance.DoSomethingPrivate();
            }
    }
    Even though DoSomethingPrivate() is private I'm allowed to call it on a different instance within class Foo only.

    Is there a good reason why this is allowed?  A type should trust itself?

    Thanks for your help.
    Tuesday, September 22, 2009 3:39 PM

Answers

  • Encapsulation is not broken. You still keep implementation details within the class.
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    • Marked as answer by nobugz Tuesday, September 22, 2009 5:02 PM
    Tuesday, September 22, 2009 4:02 PM