Declare Field (read-only)

Declares a read-only field for an undeclared reference.

Availability

From the context menus or via shortcuts:

  • when the edit cursor or caret is on an undeclared reference within a constructor.

Example

class ClassA
{
    public ClassA(int num)
    {_MyField = num;
    }
}
Public Class ClassA
    Public Sub New(ByVal num As Integer)_MyField = num
    End Sub
End Class

Result:

class ClassA
{
    private readonly int _MyField;
    public ClassA(int num)
    {
        _MyField = num;
    }
}
Public Class ClassA
    Private ReadOnly _MyField As Integer
    Public Sub New(ByVal num As Integer)
        _MyField = num
    End Sub
End Class