177

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

Visual Studio 2010 and C#.

sajadre
  • 1,141
  • 2
  • 15
  • 30

17 Answers17

363

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amra
  • 24,780
  • 27
  • 82
  • 92
  • 1
    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... – Hans Olsson Oct 06 '10 at 16:02
  • 1
    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. – James Jul 15 '12 at 10:49
  • 1
    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. – Bitterblue Mar 03 '14 at 07:54
  • 1
    @Eve What setting did you change? – Travis Jul 24 '14 at 19:35
  • @Travis I'm translating: Go to Options → Text-Editor → C# → IntelliSense. Disable auto completion after 1 key. That's the key setting. – Bitterblue Jul 25 '14 at 06:07
  • @Eve Hmm...thanks. Doesn't exist in my VS, but thanks for the reply. :) – Travis Jul 25 '14 at 15:16
  • @Travis How much of the path does exist in your VS ? I use VS 2010 Express and it's language is not English so you have to look around a bit and understand what I mean. In "IntelliSense" it's the very top option that must be disabled. – Bitterblue Jul 29 '14 at 13:27
  • @Eve Turns out that ReSharper is interfering with the behavior, and that's where my issue is coming from. Thanks for your help, though. :) – Travis Jul 29 '14 at 16:52
  • 1
    @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". – ScubaSteve Sep 23 '19 at 14:27
34

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

Press Ctrl + K and then X.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • This *is* actually an answer to the question as you can not only see the available snippets, but also invoke it: *"Visual C#"* → ctor → (and only slightly higher number of key presses, 9 instead of 5 (using only the keyboard)). – Peter Mortensen Sep 04 '20 at 23:04
  • See also [Marleen Schilt's answer](https://stackoverflow.com/questions/3873702/code-snippet-or-shortcut-to-create-a-constructor-in-visual-studio/31404344#31404344). – Peter Mortensen Sep 04 '20 at 23:12
  • It must be mentioned, that the correct snippet is Ctrl+K, Ctrl+X. Ctrl+K, X specifically doesn't work – NotAPro Aug 04 '21 at 17:05
15

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saulius
  • 1,736
  • 20
  • 29
9

Type ctor, and then press TAB twice.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ssmsnet
  • 2,226
  • 2
  • 17
  • 14
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

Marleen Schilt
  • 650
  • 15
  • 26
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sifou13
  • 611
  • 6
  • 4
5

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).
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alon Gweta
  • 367
  • 3
  • 8
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    I realize this is an old answer, but OMG this saved me so much typing when doing object mapping. Thank you x1000. – MadHenchbot Mar 16 '22 at 18:03
3

Simply type ctor then press TAB.

Marleen Schilt
  • 650
  • 15
  • 26
Oded
  • 489,969
  • 99
  • 883
  • 1,009
3
  1. Press Alt + Enter
  2. Select "Generate Constructor"
  3. Select required members

A parameterized constructor is generated with the selected members.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SujayM
  • 31
  • 2
2

Type ctor and Tab.

ََََََََََ

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay
  • 65
  • 9
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'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
denish
  • 21
  • 1
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • It is the same for vs-2008 and vs-2008, and I think is also the same for vs-2005 but not 100% sure. – Amra Oct 06 '10 at 14:48
2

Type ctor, and then press the Tab key.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muhammad Hani
  • 8,476
  • 5
  • 29
  • 44
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neil Justice
  • 1,325
  • 2
  • 12
  • 25
1

For Visual Studio 2017, press Ctrl + ..

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sadrag
  • 555
  • 2
  • 7
  • 19
  • 1
    I tried and it's working.. Please read this link https://learn.microsoft.com/en-us/visualstudio/ide/reference/generate-constructor?view=vs-2017 – sadrag Aug 24 '18 at 12:25
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)
{

}
Elliott
  • 2,035
  • 20
  • 23