Incorrect Auto Property Can Be Made Get Only Suggestion

I have a base class that has a property defined like this:

protected virtual string ServiceName { get; set; } = "Unknown";

Resharper suggests that the setter can be removed.  But this is incorrect, there are 9 subclasses that override and set this property.  Removing the setter will actually cause compilation errors.  The overrides look like this:

protected override string ServiceName { get; set; } = "Catalog";

Perhaps this is caused be the subclasses being located in another project within the solution?

0
2 comments

Are the properties set anywhere else in the code? If not, R# is correct - removing the setter won't cause any errors. The property initializer syntax is equivalent to setting the property in the constructor, which works fine with read-only properties.

0

I did leave out one important part that I forgot about.  One of the sub-classes does set the property's value in a public method.  If you remove the setter in the base class, you must also remove the setter in the sub-classes.  This will cause a compilation error in the one sub-class that sets the value of the property programmatically.

0

Please sign in to leave a comment.