C# Automatic Properties

前端 未结 11 1111
有刺的猬
有刺的猬 2020-12-05 03:57

I\'m a bit confused on the point of Automatic properties in C# e.g

public string Forename{ get; set; }

I get that you are saving code by no

11条回答
  •  失恋的感觉
    2020-12-05 04:38

    Not all properties need get/set logic. If they do, you use a private variable. For example, in a MV-something pattern, your model would not have much logic. But you can mix and match as needed.

    If you were to use a field like you suggested in place of a property, you can't for example define an interface to describe your class correctly, since interfaces cannot contain data fields.

提交回复
热议问题