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

前端 未结 11 751
天涯浪人
天涯浪人 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

    Automatic properties in C#, when compiled, end up functionally exactly the same as the above code. The compiler generates a backing field for you.

    So you do need something backing a property - a field or some code - it's just that it's taken care of for you with the shortcut of auto-properties.

提交回复
热议问题