What does the “private” modifier do?

2020-04-01 08:42发布

Considering "private" is the default access modifier for class Members, why is the keyword even needed?

13条回答
\"骚年 ilove
2楼-- · 2020-04-01 08:48

Explicitness. I never use the default and always explicitly add the modifier.

This could be because of my Java background where the default was 'package' (roughly equivalent to 'internal' in C#) and so the difference always bothered me. I found explicitness to be preferable.

I also use ReSharper now which defaults to being explicit, so it only confirms and reinforces my bias :)

查看更多
倾城 Initia
3楼-- · 2020-04-01 08:54

There's a certain amount of misinformation here:

"The default access modifier is not private but internal"

Well, that depends on what you're talking about. For members of a type, it's private. For top-level types themselves, it's internal.

"Private is only the default for methods on a type"

No, it's the default for all members of a type - properties, events, fields, operators, constructors, methods, nested types and anything else I've forgotten.

"Actually, if the class or struct is not declared with an access modifier it defaults to internal"

Only for top-level types. For nested types, it's private.

Other than for restricting property access for one part but not the other, the default is basically always "as restrictive as can be."

Personally, I dither on the issue of whether to be explicit. The "pro" for using the default is that it highlights anywhere that you're making something more visible than the most restrictive level. The "pro" for explicitly specifying it is that it's more obvious to those who don't know the above rule, and it shows that you've thought about it a bit.

Eric Lippert goes with the explicit form, and I'm starting to lean that way too.

See http://csharpindepth.com/ViewNote.aspx?NoteID=54 for a little bit more on this.

查看更多
走好不送
4楼-- · 2020-04-01 08:56

Actually, if the class or struct is not declared with an access modifier it defaults to internal.

So if you want to make it private, use private.

查看更多
爷的心禁止访问
5楼-- · 2020-04-01 08:57

Private is only the default for methods on a type, but the private modifier is used elsewhere.

From C# Language Specification 3.0 (msdn) Section 3.5.1

Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility.

  • Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations.
  • Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.
  • Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
  • Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
  • Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations.
  • Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.
查看更多
看我几分像从前
6楼-- · 2020-04-01 09:00

For symmetry and to conform with coding styles that like everything to be explicit (personally I like it ...)

查看更多
神经病院院长
7楼-- · 2020-04-01 09:00

As Robert Paulson said in his answer, the private modifier is not just used on members, but also on types. This becomes important because the default for types is internal which can leak unintentionally if you use the InternalsVisibleToAttribute.

查看更多
登录 后发表回答