Using a backing variable for getters and setters

前端 未结 6 1179
北荒
北荒 2020-12-20 13:10

Perhaps this is a silly question, however, I am resonable new to C# (more from a Java background) and have got confused between different examples I have seen regarding gett

6条回答
  •  情书的邮戳
    2020-12-20 14:10

    I do not really see the benefit of using these backing variables (_something) unless of course you have some complex logic regarding the setting of the variables.

    There is no advantage if you're not using it. With the second approach, there is still a backing variable, but you're letting the compiler do the work of adding it. As of .NET 3.5 and later, your current approach is perfectly valid.

    Of course, as soon as you need to introduce extra logic, then managing the backing store yourself becomes critical.

提交回复
热议问题