views:

118

answers:

3

My workmate always tells me that if we declare anything as "public" then it is dangerous because then any program can access that memory and that the solution is to use the "private" access modifier.

I am wondering if this is infact true.

+7  A: 

That is not, in fact, true.

Access modifiers are only there to help organize your code. They only protect it in the sense that you protect your glass from being knocked over by setting it out of reach of the cat.

Joviee
+1 to Joviee - Public / Private are class / member access specificiers. What your workmate might be intending to convey, though, is that if ur class / members are declared as public, they can in fact be accessed by any other class directly which might not be desired.
InSane
+1 for truth, but added some practical explanation for those not understanding "why".
Rex M
Thanks Rex M. Great analogy, too!
Joviee
A: 

C# modifiers have no influence over memory adressability - the processor and OS architecture control that.

cdonner
If it is the case, this would be reasonably funny. If it's not the case, it's very demeaning and rude.
Rex M
Funny, your gravatar does not show the superiority of your neckbeard. In fact, it appears you have no neckbeard whatsoever! Makes me suspect your braggadocio is hiding something...
Will
+1: I wonder the same thing.
Greg D
+1  A: 

public and private access modifiers only have to do with the visibility of those structures (classes, method, or variables) to other classes within the same application. Memory protection between processes and users is enforced by the operating system. In the case of Windows, it does ensure that non-administrator level (and system ring) processes/threads do not have access to memory that is not explicitly shared (such as shared memory) with open permissions. Actually, Windows allows processes to grant very specific rights to specific areas of memory, but this is not provided in the language definition of C#. You would need to access system APIs to control grant that kinds of access to specific blocks of memory; by default all blocks of memory are protected by the OS.

Now, if the memory scanner is running in ring-0 or with specific elevated privileges there is nothing you can do in your process to block that access.

Kevin Brock