Refactorings specific to auto-implemented properties
Auto-implemented properties enable you to quickly specify a property without having to write logic for the property accessors. The auto-property’s logic and the field serving as a backing store are automatically generated by the compiler. Such properties appeared in C# version 3.0 and Visual Basic version 10.0.
Refactor! Pro has several refactorings that allow you to convert “full” properties with a backing store field into auto-implemented properties and vice versa. They are Convert to Auto-implemented Property and Create Backing Store.
The Convert to Auto-implemented Property refactoring is available for properties that encapsulate a private field without additional logic inside accessors, for example:
private int _Name; public int Name { get { return _Name; } set { _Name = value; } }
Private _name As String Public Property Name As String Get Return _name End Get Set(ByVal Value As String) _name = Value End Set End Property
The refactoring is available here showing you a preview hint of the resulting code:
After the refactoring is executed, you get a simple auto-implemented property:
public string Name { get; set; }
Visual Basic:
Public Property Name() As String
The refactoring performs the following actions automatically:
- removes the backing store field for the property;
- replaces the original property with an auto-implemented property;
- replaces all field references to references to the new property;
There’s a second version of the refactoring called Convert to Auto-implemented Property (convert all) which does absolutely the same actions, but for all properties in the current type declaration that can to be converted into auto-properties.
The opposite refactoring, called Create Backing Store, converts an auto-implemented property into a full property with a field serving as a backing store. This can be useful if you’d like to add additional logic for one of the property’s accessors. Or, if you’re going to prevent an auto-implemented property from being serialized, because auto-properties don’t support the [NonSerializable] attribute.
Here’s a sample preview hint of the Create Backing Store refactoring: