177

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

Visual Studio 2010 and C#.

17 Answers 17

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.

9
  • 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... Oct 6, 2010 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, 2012 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 3, 2014 at 7:54
  • 1
    @Eve What setting did you change?
    – Travis
    Jul 24, 2014 at 19:35
  • 2
    @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, 2019 at 14:27
34

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

Press Ctrl + K and then X.

3
  • 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)). Sep 4, 2020 at 23:04
  • See also Marleen Schilt's answer. Sep 4, 2020 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 4, 2021 at 17:05
16

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

9

Type ctor, and then press TAB twice.

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

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

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).
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
  • 1
    I realize this is an old answer, but OMG this saved me so much typing when doing object mapping. Thank you x1000. Mar 16, 2022 at 18:03
3

Simply type ctor then press TAB.

1
  • Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration). Jun 3, 2019 at 12:45
3
  1. Press Alt + Enter
  2. Select "Generate Constructor"
  3. Select required members

A parameterized constructor is generated with the selected members.

0
2

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

2
  • 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 6, 2010 at 14:48
  • 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
    – bkwdesign
    Oct 25 at 17:05
2

Type ctor, and then press the Tab key.

1
  • Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration). Jun 3, 2019 at 12:46
2

Type ctor and Tab.

ََََََََََ

1
  • Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration). Jun 3, 2019 at 12:46
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'.

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

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

}
1

For Visual Studio 2017, press Ctrl + ..

1

Your Answer

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