Code snippet or shortcut to create a constructor in Visual Studio
Asked Answered
L

17

178

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

Visual Studio 2010 and C#.

Lugubrious answered 6/10, 2010 at 14:42 Comment(0)
A
364

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.

Abandon answered 6/10, 2010 at 14:45 Comment(9)
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...Eous
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.Wellwisher
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.Talion
@Eve What setting did you change?Eisk
@Eisk I'm translating: Go to Options → Text-Editor → C# → IntelliSense. Disable auto completion after 1 key. That's the key setting.Talion
@Eve Hmm...thanks. Doesn't exist in my VS, but thanks for the reply. :)Eisk
@Eisk 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.Talion
@Eve Turns out that ReSharper is interfering with the behavior, and that's where my issue is coming from. Thanks for your help, though. :)Eisk
@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".Subirrigate
P
34

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

Press Ctrl + K and then X.

Pix answered 28/5, 2015 at 7:23 Comment(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)).Malathion
See also Marleen Schilt's answer.Malathion
It must be mentioned, that the correct snippet is Ctrl+K, Ctrl+X. Ctrl+K, X specifically doesn't workButterfat
T
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

Thersathersites answered 19/6, 2019 at 11:8 Comment(0)
W
10

Type ctor, and then press TAB twice.

Whelk answered 28/10, 2014 at 11:0 Comment(0)
T
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

Throat answered 14/7, 2015 at 10:41 Comment(0)
B
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.

Burned answered 4/2, 2013 at 17:7 Comment(0)
H
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).
Hell answered 20/6, 2019 at 9:20 Comment(0)
B
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.

Breather answered 10/11, 2018 at 19:14 Comment(1)
I realize this is an old answer, but OMG this saved me so much typing when doing object mapping. Thank you x1000.Lend
J
3

Simply type ctor then press TAB.

Jurdi answered 6/10, 2010 at 14:46 Comment(1)
Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).Malathion
S
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

Syconium answered 6/9, 2017 at 14:57 Comment(0)
R
3
  1. Press Alt + Enter
  2. Select "Generate Constructor"
  3. Select required members

A parameterized constructor is generated with the selected members.

Reiser answered 14/11, 2019 at 11:19 Comment(0)
E
2

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

Ebsen answered 6/10, 2010 at 14:46 Comment(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.Abandon
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 @Subirrigate comment elsewhere on this question) - restarted V.S. and it's still not workingCenac
K
2

Type ctor, and then press the Tab key.

Kun answered 6/10, 2010 at 14:49 Comment(1)
Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).Malathion
H
2

Type ctor and Tab.

ََََََََََ

Hisakohisbe answered 11/10, 2012 at 16:30 Comment(1)
Two TABs (instead of one) are necessary in at least one version of Visual Studio (possibly depending on configuration).Malathion
F
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'.

Franconian answered 5/6, 2013 at 9:29 Comment(0)
S
1

For Visual Studio 2017, press Ctrl + ..

Solve answered 20/10, 2017 at 12:21 Comment(1)
I tried and it's working.. Please read this link learn.microsoft.com/en-us/visualstudio/ide/reference/…Solve
T
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)
{

}
Timepiece answered 28/5, 2019 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.