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.
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.
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.
C# modifiers have no influence over memory adressability - the processor and OS architecture control that.
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.