Any reason to write the “private” keyword in C#?

后端 未结 9 565
梦谈多话
梦谈多话 2020-12-04 18:54

As far as I know, private is the default everywhere in C# (meaning that if I don\'t write public, protected, internal

相关标签:
9条回答
  • 2020-12-04 19:21

    A lot of people (people like me!) regularly program in a handful of different languages. Being explicit with things like these prevents me from needing to remember all the arcane details of all the languages I program in.

    0 讨论(0)
  • 2020-12-04 19:25

    private adds visual clutter. To those who insist that it makes things explicit, I would ask: Do you do this with mathematics, too? For instance:

    var answer = a + b / c;
    

    Do you find that unclear without redundant parentheses around b / c?

    The rule in C# is very simple: By default, everything is as close to private as it can be. So if you need something to be more visible than the default, add a modifier. Otherwise, don't add needless keywords to your code.

    0 讨论(0)
  • 2020-12-04 19:27

    As far as I know, private is the default everywhere in C#

    Explicitly declaring private, means you know it is private. Not just think it is, because as far as you know, it is the default. It also means that someone else who looks at the code knows what it is.

    There is no "I think it is", "I'm pretty sure it is", etc. It just is. And everyone is on the same page.

    I am not a C# developer. If I had to work with some code that wasn't explicitly declared private, I would probably assume it was internal.

    I dislike when things are implicitly set. It's never as clear as when they are explicitly set.

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