On the menu bar, choose Tools > Options. Expand Environment, and then choose Keyboard. Optional: Filter the list of commands by entering all or part of the name of the command, without spaces, in the Show commands containing box. In the list, choose the command to which you want to assign a keyboard shortcut.
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.
Code snippets are ready-made snippets of code you can quickly insert into your code. For example, the for code snippet creates an empty for loop. Some code snippets are surround-with code snippets, which enable you to select lines of code, and then choose a code snippet which incorporates the selected lines of code.
Type "ctor" + TAB + TAB (hit the Tab key twice). This will create the default constructor for the class you are in:
public MyClass()
{
}
It seems that in some cases you will have to press TAB twice.
If you want to see the list of all available snippets:
Press Ctrl + K and then X.
Type ctor
, and then press TAB twice.
In case you want a constructor with properties, you need to do the following:
Place your cursor in any empty line in a class;
Press Ctrl + . to trigger the Quick Actions and Refactorings menu;
Select Generate constructor from the drop-down menu;
Pick the members you want to include as constructor parameters. You can order them using the up and down arrows. Choose OK.
The constructor is created with the specified parameters.
Generate a constructor in Visual Studio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With