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

后端 未结 9 1925
失恋的感觉
失恋的感觉 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 02:00

    Since no one mentioned it... you could make the auto-property virtual and override it:

    public virtual int Property { get; set; }
    

    If there was no get/set, how would it be overridden? Note that you are allowed to override the getter and not the setter:

    public override int Property { get { return int.MinValue; } }
    

提交回复
热议问题