Why explicitly write “private”?

前端 未结 8 1727
孤城傲影
孤城傲影 2020-12-17 08:46

As fields are implicitly private, why there is often explicit declaraion used in the books, articles etc.?

相关标签:
8条回答
  • 2020-12-17 09:04

    To make your code look nice :).

    You're right it's not necessary, but it's custom to write them anyway. At the very least, every method has a privilege explicitly noted and it makes your code easier to read.

    0 讨论(0)
  • 2020-12-17 09:05

    The problem with implicit declarations is that the reader cannot tell if whoever wrote the code wanted the implicit declaration or simply forgot to write anything. By being explicit there's no doubt about the intentions.

    0 讨论(0)
  • 2020-12-17 09:06

    Explicit declaration used to told you that the variable is deliberately set to private and it needs to be declare as private

    0 讨论(0)
  • 2020-12-17 09:09

    Sometimes explicit is better than implicit, and this is even more so when you are writing educational material. For people who do not know or cannot remember the rules for the default access levels it is one less thing for them to be concerned with when reading the code.

    Related Question

    • What are the Default Access Modifiers in C#?

    The default access for everything in C# is "the most restricted access you could declare for that member".

    0 讨论(0)
  • 2020-12-17 09:14

    Because you write code for maintainability and clarity, ESPECIALLY in code samples. Implicit declarations are there for the compiler, not for the programmer. Failing to explicitly declare the visibility and scope of your variables leaves your intent ambiguous. Is it really that much extra typing?

    0 讨论(0)
  • 2020-12-17 09:15

    Because default access levels vary across languages, and many people program in more than one language. It's easy to become confused, either as the author or as someone reading the code later, thus explicit is nicer to deal with than implicit.

    0 讨论(0)
提交回复
热议问题