Encapsulation C# newbie

前端 未结 5 984
我在风中等你
我在风中等你 2021-01-02 22:54

New to C#, and I understand that encapsulation is just a way of \"protecting data\". But I am still unclear. I thought that the point of get and set accessors wer

5条回答
  •  遥遥无期
    2021-01-02 23:20

    Because it is easier to change the Code if you want to add the checks/tests later on. Especially if you have many inheritance and many classes in your code it is very hard to change the implementation from a public variable to a public Property.

    Moreover you can add to the get and set within the property different attributes, e.g. if you are using reflection. The get and set of the property are internally different methods. If you have just a public variable /field it is not possible to added different properties to the different access ways.

提交回复
热议问题