Skip to content

Encapsulate field as read-only or write-only #26

@giggio

Description

@giggio

From a field create a property with getter or setter and optionally change all the references to that field to use the property. There is a refactoring already in Visual Studio for a full encapsulation.

class Foo
{
    private int i;
    void Bar()
    {
        i = 1;
    }
    void Baz()
    {
        var j = i;
    }
}

If changed to read-only:

class Foo
{
    private int i;
    public int I => i;
    void Bar()
    {
        i = 1;
    }
    void Baz()
    {
        var j = I;
    }
}

If changed to write-only:

class Foo
{
    private int i;
    public int I
    {
        i = value;
    }
    void Bar()
    {
        I = 1;
    }
    void Baz()
    {
        var j = i;
    }
}

If the field is not private and has references outside the class, change those as well.

This is essentially a refactoring, so we are classifying it as a Hidden diagnostic.
Diagnostic id: CC0058
Category: Refactoring

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions