Skip to main content

Declare Property

  • 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.

screencast

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.

    public class TestClass
    {
        public void TestMethod()
        {
            ClassA obj = new ClassA { Prop1 = 12, Prop2 = "Name" };
        }
    }
    
    public class ClassA
    {       
        public string Prop2 { get; set; }
    }
    
  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.

    declare-properties-menu

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

    • Auto-Implemented Property
    • Property with Field
    • Property with Default Body

    Press Enter to apply the selected code provider.

    A red target picker marker appears. This marker allows you to choose the place where the generated code can be inserted.

    target-picker

    You can configure the Target Picker on the Editor | All Languages | Code Actions | Target Picker options page.

    target-picker-options

  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-auto-property-screencast

    This code provider generates an auto-implemented property.

  • Declare Property with Field

    declare-property-with-field-screencast

    This code provider generates a property with a backing store.

  • Declare Property with Default Body

    declare-property-with-body-screencast

    This code provider generates a property that throws the NotImplemented exception.

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.

    DeclarePropertyField

  • From Razor markup.

    DeclarePropertyField

    If a Razor code-behind file (.razor.cs) exists, these code providers add the property declaration to this file instead of the @code section.

    DeclarePropertyField

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.

Page

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

Page

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

declare-with-body-code-actions

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.

Options_Scope

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:

int Prop1 { get; set; }

See the following topic for details: Scope.