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

后端 未结 9 576
梦谈多话
梦谈多话 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: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.

提交回复
热议问题