Code Inspection: Convert property to auto-property
If the get
and set
accessors of a property do nothing but return or set value to a backing field, JetBrains Rider suggests to convert 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.
Here is an example of a quick-fix suggested by this inspection:
Suboptimal code | After the quick-fix |
---|---|
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, e.g., if the property name is used in another context, you may need to resolve conflicts that arise.
Last modified: 27 December 2017