ReSharper 2024.3 Help

Code inspection: Convert property into auto-property

If the get and set accessors of a property do nothing but return or set value to a backing field, ReSharper suggests converting this property into an auto-implemented property (also known as auto-property), remove the backing field and replace all its usages with the newly created auto-property.

The inspection will not highlight properties in classes marked with the [Serializable] attribute, because the serializer may rely on the existing fields.

Here is an example of a quick-fix suggested by this inspection:

class Shape { private Color bgColor; public Color BackgroundColor { get { return bgColor; } set { bgColor = value; } } public Shape(Color background) { bgColor = background; } }
class Shape { public Color BackgroundColor { get; set; } public Shape(Color background) { BackgroundColor = background; } }

Note that this quick-fix invokes the Convert Property to Auto-Property refactoring, which will replace all usages of the backing field solution-wide. In some cases, for example if the property name is used in another context, you may need to resolve conflicts that arise.

Last modified: 08 April 2024