10

Our Application is an MVC Application . I tried to run code analysis using ReSharper. I am getting "Auto-property accessor is never used" as warnings in many of my view model properties.

For example, ReSharper shows the warning on this:

public bool IsLegalEntry { get; set; }

Can I make a private setter, or can anybody suggest an alternative?

bubbleking
  • 3,329
  • 3
  • 29
  • 49
Sri vahgar
  • 109
  • 1
  • 8

2 Answers2

7

You could make the setter private

public bool IsLegalEntry { get; private set; }

However, this could cause a runtime error if the setter is used implicitly. Alternatively you could decorate the setter with the JetBrains.Annotations.UsedImplicitlyAttribute.

public bool IsLegalEntry { get; [UsedImplicitly] set; }
Rachelle
  • 101
  • 2
  • 6
1

Alternative to "warning fixing/suppressing" paradigm, you can add testing project to your solution. Then write a test for you business logic hitting the accessor(s) in question among other things.

Not sure this is the action you were looking for, however

  • it provides the effect you were after
  • saves for redundant annotation attributes
  • adds testing bonus
Sergiy Yeskov
  • 227
  • 2
  • 3
  • 1
    Be careful not to add tests that don't add value just to suppress a warning. Testing that calling get on a auto property after calling set for the same auto property is pretty useless and just adds cruft to your project. – Steven Benitez Feb 17 '23 at 19:05