Code-snippet or short cut to create a constructor

2019-03-08 08:51发布

问题:

What is the code-snippet or shortcut to create a constructor in Visual Studio?

Visual Studio 2010 and C#.

I've used it before, but I can't remember.

回答1:

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.



回答2:

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

Press Ctrl + K and then X.



回答3:

Type ctor, and then press TAB twice.



回答4:

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



回答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.



回答6:

Simply type ctor then press TAB.



回答7:

Type ctor, and then press the Tab key.



回答8:

Type ctor and Tab.

ََََََََََ



回答9:

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



回答10:

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



回答11:

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:



回答12:

For Visual Studio 2017 press "Ctrl + ."



回答13:

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.