What is the code snippet or shortcut for creating a constructor in Visual Studio?
Visual Studio 2010 and C#.
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
Simply type ctor
then press TAB.
I don't know about Visual Studio 2010, but in Visual Studio 2008 the code snippet is 'ctor'.
Type ctor
and Tab.
ََََََََََ
Should you be interested in creating the 'ctor' or a similar class-name-injecting snippet from scratch, create a .snippet file in the C# snippets directory (for example C:\VS2017\VC#\Snippets\1033\Visual C#\C#Snippets.snippet
) with this XML content:
<CodeSnippets>
<CodeSnippet>
<Header>
<Title>ctor</Title>
<Shortcut>ctor</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false"><ID>classname</ID><Function>ClassName()</Function></Literal>
</Declarations>
<Code>
<![CDATA[public $classname$($end$)
{
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
This snippet injects the current class name by way of calling C# code snippet function ClassName(), detailed on this docs.microsoft page.
The end result of expanding this code snippet:
In Visual Studio 2010, if you type "ctor" (without the quotes), IntelliSense should load, showing you "ctor" in the list. Now press TAB twice, and you should have generated an empty constructor.