Back to Devexpress

Declare Property

coderushforroslyn-115703-coding-assistance-code-providers-declaration-providers-declare-property.md

latest3.6 KB
Original Source

Declare Property

  • Aug 25, 2021
  • 3 minutes to read

Purpose

Declares a property for a selected reference to an undeclared element. You can use this code provider in Test-Driven Development.

This code provider can also drop a marker onto the selected property reference, if the Marker feature is enabled. See the following topic section for more details: Markers: How to Enable.

Availability

Available when the caret is in an undeclared variable reference.

Usage

  1. Place the caret in an undeclared variable reference.

  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.

  3. Select the Declare menu and choose one of the following providers to declare the corresponding property:

  4. Use the Up Arrow and Down Arrow keys to move the target picker.

  5. Press Enter to generate a code in the selected place.

The generated code depends on the selected code provider and the property used in the original code. If you call the code provider for a variable contained in the right part of an expression, this provider generates a read-only property; otherwise, this provider declares a read-write property.

The following screencasts show the result of applying code providers from the Code Actions menu:

  • Declare Auto-Implemented Property

  • Declare Property with Field

  • Declare Property with Default Body

Blazor Support

You can apply the Declare Property (auto-implemented), Declare Property (with field), and Declare Property (with default body) code providers from the following places in .razor files:

  • From @code sections.

  • From Razor markup.

Customization

Change Code Actions Settings

You can configure the “Default body of newly-generated methods” setting for the Declare Property with Default Body code provider on the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options page.

For example, set the “Default body of newly-generated methods” setting to “Return default value”, as shown below:

Run the Declare Property with Default Body code provider. The screencast below shows this setting in action:

For more information, refer to the following topic: Code Actions Settings.

Change Scope

The Declare Property provider can create a property with the specified scope. The default scope of the generated property is Public. To change the visibility modifier:

Open the Editor | C# (Visual Basic) | Scope Options options page.

Set the default scope for a declared property to “Private” and run the Declare Auto-Implemented Property provider.

This provider creates a property with the private visibility, as shown below:

csharp
int Prop1 { get; set; }
vb
Private Property Prop1 As Integer

See the following topic for details: Scope.