.NET Framework Bookmark and Share   
 index > Common Language Runtime > Within a class, calling a private method of the same class
 

Within a class, calling a private method of the same class

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.
Neil Blackburn
Encapsulation is not broken. You still keep implementation details within the class.
Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
Vitaliy Liptchinsky
Encapsulation is not broken. You still keep implementation details within the class.
Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
Vitaliy Liptchinsky

You can use google to search for other answers

Custom Search

More Threads

• Not showing command prompt window on a system() call
• pinvoke and enum
• unmanaged dll, struct runtime issues
• GAC Assemblies missing under Vista
• TlbExp ignore referenced assembly
• Interop marshalling managed object to a void*?
• Problem Hosting .NET Win UserControl in Internet Explorer in ASP.NET project. System.IO.FileLoadException
• Help kill Excel.exe!
• GetHdevmode and DEVMODE struct
• Exception from background thread kill webserver