Getters, setters, and properties best practices. Java vs. C#

前端 未结 12 1484
一整个雨季
一整个雨季 2020-12-02 06:32

I\'m taking a C# class right now and I\'m trying to find out the best way of doing things. I come from a Java background and so I\'m only familiar with Java best-practices;

12条回答
  •  醉梦人生
    2020-12-02 07:24

    In C# favor properties for exposing private fields for get and/or set. The thie form you mention is an autoproperty where the get and set automatically generate a hidden pivot backing field for you.

    I favor auto properties when possible but you should never do a set/get method pair in C#.

提交回复
热议问题