Skip to content

Introduce field from constructor #241

@giggio

Description

@giggio

This is a refactoring.

The idea is that on a constructor parameter you CTRL DOT and get a "Introduce field" dialog, that will create a field and assign the value from the parameter to the field.

So this:

class Foo
{
  public Foo(string bar)
  {
  }
}

Becomes:

class Foo
{
  private readonly string bar;
  public Foo(string bar)
  {
    this.bar = bar;
  }
}

If the field is already assigned then no diagnostic is offered.
If the field is already present, it is just assigned to.
If the type does not match (e.g. parameter is string, existing field is int, a new field with 1 postfixed is added, like that:

class Foo
{
  private int bar;
  public Foo(string bar)
  {
  }
}

Becomes:

class Foo
{
  private int bar;
  private readonly string bar1;
  public Foo(string bar)
  {
    this.bar1 = bar;
  }
}

Diagnostic Id: CC0071
Severity: Hidden (refactoring)
Category: Refactoring

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions