C# Automatic Properties - Why Do I Have To Write “get; set;”?

后端 未结 9 1979
失恋的感觉
失恋的感觉 2020-12-29 01:33

If both get and set are compulsory in C# automatic properties, why do I have to bother specifying \"get; set;\" at all?

9条回答
  •  感动是毒
    2020-12-29 01:55

    Because you might want a read-only property:

    public int Foo { get; private set; }
    

    Or Write-only property:

    public int Foo { private get; set; }
    

提交回复
热议问题