开发者

Can auto implemented properties have a private set?

开发者 https://www.devze.com 2023-02-04 05:18 出处:网络
I tried to do this: public int Property {get; private set;} It underlines the \"set\" and says this: auto implemented accessor never set

I tried to do this:

public int Property {get; private set;}

It underlines the "set" and says this:

auto implemented accessor never set

It lets me compile but I am wondering if this effects anything or what is开发者_开发技巧 the deal with it?


What you have is valid syntax. You are getting the warning because nothing is setting a value to the property (ie. the setter is not being used anywhere).

If nothing is using the setter, then the property will always have its default value, in which case you should question the value of having the property there in the first place.


That's just a compiler warning not an error. It just tells you that you never use the setter inside the class.


The setter is private and compiler can check usage of it in the current class. The warning was generated because you never set a value into that property.

0

精彩评论

暂无评论...
验证码 换一张
取 消