What is the code snippet or shortcut for creating a constructor in Visual Studio?
Visual Studio 2010 and C#.
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.
ctor only shows the intellisense box, then I have to type Tab once to close that and once to "use" the snippet), but might be some setting somewhere...Tab work. ctor works for me with 1 hit though.If you want to see the list of all available snippets:
Press Ctrl + K and then X.
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.
For the full list of snippets (little bits of prefabricated code) press Ctrl+K and then Ctrl+X. Source from MSDN. Works in Visual Studio 2013 with a C# project.
So how to make a constructor
Update: You can also right-click in your code where you want the snippet, and select Insert Snippet from the right-click menu
As mentioned by many, "ctor" and double TAB works in Visual Studio 2017, but it only creates the constructor with none of the attributes.
To auto-generate with attributes (if there are any), just click on an empty line below them and press Ctrl + .. It'll display a small pop-up from which you can select the "Generate Constructor..." option.
Simply type ctor then press 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:
I don't know about Visual Studio 2010, but in Visual Studio 2008 the code snippet is 'ctor'.
ctor forever in many versions of Visual Studio. I am contracting on a customer-provisioned laptop with VS 2019, and googled my way here because ctor is not working, even though I have now gone in and changed the "Snippets Behavior" (based on @ScubaSteve comment elsewhere on this question) - restarted V.S. and it's still not workingType ctor, and then press the Tab key.
Type ctor and Tab.
ََََََََََ
I have created some handy code snippets that'll create overloaded constructors as well. You're welcome to use them: https://github.com/ejbeaty/Power-Snippets
For example: 'ctor2' would create a constructor with two arguments and allow you to tab through them one by one like this:
public MyClass(ArgType argName, ArgType argName)
{
}
For Visual Studio 2017, press Ctrl + ..