chobo2
chobo2

Reputation: 85765

Can auto implemented properties have a private 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?

Upvotes: 9

Views: 7595

Answers (3)

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5833

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.

Upvotes: 1

adrianbanks
adrianbanks

Reputation: 82944

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.

Upvotes: 14

Vadim
Vadim

Reputation: 17957

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

Upvotes: 5

Related Questions