Does declaring a variable as "private" in C# protect the memory in windows from being accessed by a memory scanner?
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 inf开发者_开发知识库act 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.
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.
C# modifiers have no influence over memory adressability - the processor and OS architecture control that.
精彩评论