What for should I mark private variables as private if they already are?

前端 未结 10 1681
星月不相逢
星月不相逢 2021-01-06 18:43

As far as I know, in C# all fields are private for default, if not marked otherwise.

class Foo
{
  private string bar;
}

class Foo
{
  string bar;
}
         


        
10条回答
  •  迷失自我
    2021-01-06 19:41

    Now; fields should pretty-much always be private anyway, so it is an edge case whether you should bother.

    For the wider subject, I remember a comment by Eric Lippert - essentially saying that given a method / class / whatever:

    void Foo() {}
    class Bar {}
    

    Then it isn't clear whether they are private/internal deliberately, or whether the developer has thought about it, and decided that they should be private/internal/whatever. So his suggestion was: tell the reader that you are doing things deliberately instead of by accident - make it explicit.

提交回复
热议问题