Difference between Automatic Properties and public field in C# 3.0

后端 未结 7 1155
忘掉有多难
忘掉有多难 2020-12-01 23:50

I failed to understand why auto implemented property language feature exist in C# 3.0.

What the difference it is making when you say

public string Fi         


        
7条回答
  •  北海茫月
    2020-12-02 00:15

    Consider what happens if you later want to change each of them to a property with a custom implementation. If it's an automatically implemented property, you just add a field and change the implementation. Full source and binary compatibility.

    If it's a field to start with, you get neither source nor binary compatibility. You have to rebuild everything that references it, and fix up anything which no longer compiles.

    Additionally, properties have various benefits over fields. My main personal objection to fields is that it exposes an implementation decision in the API.

提交回复
热议问题