Difference between Automatic Properties and public field in C# 3.0

后端 未结 7 1163
忘掉有多难
忘掉有多难 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:24

    The difference is that other assemblies compiled with code that read the property is compiled against a property.

    If you later on decide you need to add code to the getter or setter, you can do that, without having to force every other assembly linked to it to recompile.

    Not so with fields. If you later on change a field to be a property, in order to add that code, other assemblies linked against yours will cease to function properly since they're compiled to read a field, not a property.

    Also, lots of code is written to find properties, not fields, like data binding and similar.

提交回复
热议问题