Why do I need a private field that is exposed via public property?

前端 未结 11 732
天涯浪人
天涯浪人 2020-12-11 11:41

I\'m comming from Java world mainly. So, C# properties do look nice.

I know that with C# 3.0 or above I can use Automatic Properties. I like it even more :).

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 12:04

    Getters and Setters are the public interface that other classes interact with. In complex systems you usually end up doing validation and other work inside the getter and setter.

    The private field is for internal use. If you need to change the value from inside the class but bypass all the extra work you would change the private variable.

提交回复
热议问题