Code snippet or shortcut to create a constructor in Visual Studio

C#Visual StudioConstructorCode SnippetsShortcut

C# Problem Overview


What is the code snippet or shortcut for creating a constructor in Visual Studio?

Visual Studio 2010 and C#.

C# Solutions


Solution 1 - 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.

Solution 2 - C#

If you want to see the list of all available snippets:

Press Ctrl + K and then X.

Solution 3 - C#

In case you want a constructor with properties, you need to do the following:

  1. Place your cursor in any empty line in a class;

  2. Press Ctrl + . to trigger the Quick Actions and Refactorings menu;

    Refactoring menu

  3. Select Generate constructor from the drop-down menu;

  4. 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

Solution 4 - C#

Type ctor, and then press TAB twice.

Solution 5 - C#

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

  1. Press Ctrl+K and then Ctrl+X
  2. Select Visual C#
  3. Select ctor
  4. Press Tab

Update: You can also right-click in your code where you want the snippet, and select Insert Snippet from the right-click menu

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

Simply type ctor then press TAB.

Solution 9 - C#

I don't know about Visual Studio 2010, but in Visual Studio 2008 the code snippet is 'ctor'.

Solution 10 - C#

Type ctor, and then press the Tab key.

Solution 11 - C#

Type ctor and Tab.

ََََََََََ

Solution 12 - C#

Type the name of any code snippet and press TAB.

To get code for properties you need to choose the correct option and press TAB twice because Visual Studio has more than one option which starts with 'prop', like 'prop', 'propa', and 'propdp'.

Solution 13 - C#

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:

'ctor' snippet

Constructor end result

Solution 14 - C#

If you use ReSharper, you can quickly generate constructors by typing:

  • 'ctor' + Tab + Tab (without parameters),
  • 'ctorf' + Tab + Tab (with parameters that initialize all fields) or
  • 'ctorp' + Tab + Tab (with parameters that initialize all properties).

Solution 15 - C#

  1. Press Alt + Enter
  2. Select "Generate Constructor"
  3. Select required members

A parameterized constructor is generated with the selected members.

Solution 16 - C#

For Visual Studio 2017, press Ctrl + ..

Solution 17 - C#

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)
{

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser467058View Question on Stackoverflow
Solution 1 - C#AmraView Answer on Stackoverflow
Solution 2 - C#ANewGuyInTownView Answer on Stackoverflow
Solution 3 - C#SauliusView Answer on Stackoverflow
Solution 4 - C#ssmsnetView Answer on Stackoverflow
Solution 5 - C#Marleen SchiltView Answer on Stackoverflow
Solution 6 - C#Sifou13View Answer on Stackoverflow
Solution 7 - C#Nuwan JayawardeneView Answer on Stackoverflow
Solution 8 - C#OdedView Answer on Stackoverflow
Solution 9 - C#M_J_O_N_E_SView Answer on Stackoverflow
Solution 10 - C#Muhammad HaniView Answer on Stackoverflow
Solution 11 - C#JayView Answer on Stackoverflow
Solution 12 - C#denishView Answer on Stackoverflow
Solution 13 - C#Neil JusticeView Answer on Stackoverflow
Solution 14 - C#Alon GwetaView Answer on Stackoverflow
Solution 15 - C#SujayMView Answer on Stackoverflow
Solution 16 - C#sadragView Answer on Stackoverflow
Solution 17 - C#ElliottView Answer on Stackoverflow