Home > Coding Helpers > Quick ways to add and remove a set/get property accessor with CodeRush

Quick ways to add and remove a set/get property accessor with CodeRush

August 9th, 2012

A property without a ‘set’ accessor is considered read-only:

CodeRush Add Setter code sample

You can not assign a value to this property until you have a ‘set’ accessor to this property. The set accessor is similar to a void method with an implicit parameter called value, whose type is the type of the property. If a property does not yet have a set accessor, you can easily add it using the Add Setter code provider:

CodeRush Add Setter preview

As a result, a property has a read-write access:

CodeRush Add Setter result

The code provider will automatically determine the field reference used as a backing store and assign a value to it.

You can also perform the opposite operation – convert a read-write property to a read-only property by using the corresponding Remove Setter code provider. The code provider is available when a value is never assigned to a property. In other words, the code provider safely removes the set accessor if it is not referenced in the code:

CodeRush Remove Setter preview

The latter code provider is useful to refactor and cleanup your code, eliminating undesirable property use.

If the property is write-only, having only the setter accessor, you can use the Add Getter code provider, which is similar to the Add Setter, but adds the getter accessor instead:

CodeRush Add Getter preview

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Aug/30/2012
ID: C174

Similar Posts: