Skip to main content

Remove Redundant Destructor

#Purpose

Removes the destructor if it does not contain executable code. In most cases, you don’t have to implement a destructor, as the garbage collector disposes of objects automatically.

#Availability

Available when the cursor is on a destructor declaration, provided that the destructor does not contain executable code lines.

#Usage

  1. Place the caret on an empty destructor declaration.

    Note

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

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

After execution, the Refactoring removes the destructor.

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