coderushforroslyn-116324-coding-assistance-code-providers-declaration-providers-declare-property-with-initializer.md
Declares a property for a selected method parameter and initializes it. You can use this code provider for Test-Driven Development.
Declare Property with Initializer generates a property name based on the selected method parameter name.
This provider can also drop a marker onto the selected method parameter, if the Marker feature is enabled. See the following topic section for more details: Markers: How to Enable.
Available when the caret is in a method parameter within the method signature.
Place the caret in an undeclared method parameter.
Press Ctrl + . or Ctrl +~ to invoke the Code Actions menu.
Choose Declare Property with Initializer from the menu and press Enter.
Use the Up Arrow and Down Arrow keys to move the target picker.
Press Enter to generate a code in the selected place.
After execution, this code provider adds the auto-implemented property to the current class and assigns the parameter value to it. This code provider also links a declared property and all its references. If you change any single property reference, CodeRush applies this change to other property references.
The Declare Property with Initializer code provider is available from the @code section and markup of .razor files. The following screencast shows how to run this code provider from the @code section:
Note
If a Razor code-behind file (.razor.cs) exists, the Declare Property with Initializer code provider adds the initialized property to this file instead of the @code section.
You can change the default visibility modifier of the generated property on the Editor | C# (Visual Basic) | Scope Options options page.
For example, set the default scope for a declared property to “Private”. The Declare Property with Initializer provider creates a property with the private visibility, as shown below:
int ID { get; set; }
Private Property ID As Integer
See the following topic for details: Scope.