Difference between Automatic Properties and public field in C# 3.0

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

    Just to add to what other people have said, declaring a public field, the field is accessible for read and write. declaring public automatic property, although the property is public, you can still add modifier to control the accessibility at the get/set level.

    public string FirstName { get; private set; }
    

    The user of your class sees FirstName as public property. However, he/she cannot write to it.

提交回复
热议问题