Skip to main content

Remove Redundant Constructor

  • 2 minutes to read

Purpose

Removes an empty constructor, if it has no parameters and there are no other constructors in the class. When the class does not contain a constructor, the default one is created internally. The default constructor instantiates the object and sets member variables to the default values as listed in Default Values Table.

Availability

Available when the cursor is on a constructor declaration, provided that the constructor is empty and does not contain parameters. Also, the class should contain no other constructors.

Usage

  1. Place the caret on an empty constructor declaration.

    Note

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

    public class Person {
        public Person() { }
        private int id;
        public string Name { get; set; }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Remove Redundant Constructor from the menu.

After execution, the Refactoring removes the constructor.

public class Person {
    private int id;
    public string Name { get; set; }
}
See Also