Difference between Automatic Properties and public field in C# 3.0

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

    Because they are implemented differently in the resulting IL code (and machine language). An Automatic property is still exposed as a public getter and setter, whereas a public field is just that - a single field..

    Thus, implementing an auto property allows you at some later date to change the internal behavior of either the getter or setter (like adding a validator) without recompiling or re=coding any dependant classes that use it...

    0 讨论(0)
提交回复
热议问题