Make Read-Only

Purpose

Converts a field to a read-only field. Read-only fields can be initialized either at the declaration or in a constructor, which prevents the field value from the occasional overwriting.

Availability

Available when the cursor is on a field declaration, provided that its value never changes.

Usage

  1. Place the caret on a field declaration.

    NOTE

    The blinking cursor shows the caret's position at which the Refactoring is available.

    private int CustomerID;
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Make Read-Only from the menu.

After execution, the Refactoring adds the readonly modifier to the field declaration.

private readonly int CustomerID;
See Also