Why using only setter in property declaration?

后端 未结 4 579
北荒
北荒 2021-01-26 09:32
int MyProperty { set; }

What\'s the idea for using only setter on property? If we set one property with some value, I guess it\'s very likely to read t

4条回答
  •  孤街浪徒
    2021-01-26 09:49

    It doesn't make a whole lot of sense to have an auto-property with only a setter. It can make sense to have a manually implemented property with only a setter that can then set a field used internally, but not visible externally.

    Having a set only property is very uncommon, and are often implemented with a set method instead of a property as a set-only property is not expected behavior for most developers.

    In fact, it is a compiler error for an auto-implemented property to define a set without a get. A set-only property must be manually defined.

提交回复
热议问题