Private Set or Private member?

前端 未结 7 2199
旧时难觅i
旧时难觅i 2020-12-29 05:05

I was wondering what\'s considered the C# best practice, private/protected members with public getters, or public getters with private/protected setters?

            


        
7条回答
  •  隐瞒了意图╮
    2020-12-29 05:30

    Its the same thing. In the first example, the compiler generates the backing store. In the second, you generated the backing store. Since the implementation is internal to the class, refactoring one into the other is not a big deal. Tools like Resharper make that trivial. The reason you probably haven't seen private setters that much is that its a C# 3.0 feature.

提交回复
热议问题