Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Shortcut for Automatically Creating Constructors from an Inherited Class

Say I am inheriting from a class with several "overloaded" constructors.

By any chance is there a short cut in Visual Studio which writes the constructors in the derived class with the same signatures as in the default class for me, with boilerplate code which calls MyBase.New(...) and plugs in the arguments for me?

EDIT: As far as I can find there is no such shortcut built-in, however there is one in resharper as suggested by Raymond.

like image 617
Christopher Edwards Avatar asked Jan 08 '09 22:01

Christopher Edwards


People also ask

What is shortcut for constructor in Visual Studio?

Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Generate constructor in <QualifiedName> (with properties).

Which of the following keyboard shortcut can be used to access constructor and other auto generated code?

Generate a constructor Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there. In the Generate popup, select Constructor. In the Generate dialog that appears, select type members that should be initialized in the new constructor.

Is constructor of parent class called automatically?

Using your example, the answer is: YES. The base constructor will be called for you and you do not need to add one. You are only REQUIRED to use "base(...)" calls in your derived class if you added a constructor with parameters to your Base Class, and didn't add an explicit default constructor.

Can we inherit constructors in C#?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.


1 Answers

I don't know about a shortcut in a standard Visual Studio installation, but if you install the excellent Resharper plugin from jetBrains, it is Alt-Insert, C. I don't develop without it.

like image 50
Raymond Roestenburg Avatar answered Oct 05 '22 06:10

Raymond Roestenburg