180

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

Visual Studio 2010 and C#.

17 Answers 17

367

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Not for me (typing 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...
In addition to this technique. If you make use of the class with a specific signature, you can refactor to get visual studio to generate the constructor for you. Also if this is a default constructor and you are not doing anything with it, then it is assumed to be there by default.
Once or twice is a setting thing. I had this issue with a 1-letter-snippet and changed some (IntelliSense) settings to make 1 Tab work. ctor works for me with 1 hit though.
@Eve What setting did you change?
@RayLoveless, I had the same issue that I was able to fix in VS2019. Go to Options > Text Editor > C# > IntelliSense then change "Snippets Behavior" to "Always include snippets".
|
34

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

Press Ctrl + K and then X.

3 Comments

This is actually an answer to the question as you can not only see the available snippets, but also invoke it: "Visual C#" → ctor → <Return> (and only slightly higher number of key presses, 9 instead of 5 (using only the keyboard)).
It must be mentioned, that the correct snippet is Ctrl+K, Ctrl+X. Ctrl+K, X specifically doesn't work
18

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

Comments

10

Type ctor, and then press TAB twice.

Comments

8

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

Comments

6

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

Comments

5

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.

Comments

4

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.

1 Comment

I realize this is an old answer, but OMG this saved me so much typing when doing object mapping. Thank you x1000.
3

Simply type ctor then press TAB.

1 Comment

Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).
3

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

Comments

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

A parameterized constructor is generated with the selected members.

Comments

2

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

2 Comments

It is the same for vs-2008 and vs-2008, and I think is also the same for vs-2005 but not 100% sure.
I've used 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 working
2

Type ctor, and then press the Tab key.

1 Comment

Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).
2

Type ctor and Tab.

ََََََََََ

1 Comment

Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).
2

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

Comments

1

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

}

Comments

1

For Visual Studio 2017, press Ctrl + ..

1 Comment

I tried and it's working.. Please read this link learn.microsoft.com/en-us/visualstudio/ide/reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.