开发者

What sections of a class are designated to be public?

开发者_运维问答

As it says, Which section of a class should be public? I really feel like this is a security question myself.

"What class components are typically designated as public?"


The public parts of a class are those parts you want other classes to interface with. So that could be methods, properties, delegates, etc.

The private parts of a class are those things that no other class needs to interface with and is only internal to that class.


It is not a security issue.

When the author of a class marks members as private he is not stating "you may not know" but insted "you don't have to know" .

Encapsulation is about designing (organizing) a class with an 'inside' and an 'outside' , It is not about secrets or privileges.


Public vs private class declarations has nothing to do with security. It's about information hiding for the sake of keeping a clear definition of the contract that the class claims to support with its clients (and claims to continue supporting in future revisions), and keeping implementation details that need to be handled in a specific manner (kept in sync, consistent internal state) or that may need to be changed in future implementations away from clients.

Do not think of public vs private access as a form of security. These access modifiers exist primarily in the source code and are primarily enforced by the language compiler at compile time; at runtime any program or code is capable of enumerating the public and private members of your class using the .NET type system, and calling those public and private members through reflection.

Private declarations are a social nicety, not a security.


C# access modifiers are an aspect of design and cannot really be used for security. Obviously they can help you implement some kind of security by enforcing (really just encouraging) certain access patterns (i.e. your internal APIs encourage database connections only through some kind of factory which connects with a connection string created securely or in some consistent way, or all access to passwords is through classes which are designed to properly scramble the memory and encrypt on disk), but they can't really enforce security.

Public, private, protected and internal are the access modifiers applied to classes and members and relate to the interfaces exposed and how they behave with respect to inheritance, intra and inter-assembly-level access.

What should be public are members which need access visibility outside the class (and outside the assembly). Anything else should not be exposed until its need for visibility in the interface is properly justified. In many cases, I would suggest most things be private until justified that they need to be public. Sometimes they are immediately obvious. Certainly for static members you should really be careful about exposing as public.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜