Code snippet or shortcut to create a constructor in Visual Studio

前端 未结 17 1599
悲哀的现实
悲哀的现实 2020-12-23 12:57

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

Visual Studio 2010 and C#.

相关标签:
17条回答
  • 2020-12-23 13:21

    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.

    0 讨论(0)
  • 2020-12-23 13:22

    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)
    {
    
    }
    
    0 讨论(0)
  • 2020-12-23 13:23

    For Visual Studio 2017, press Ctrl + ..

    0 讨论(0)
  • 2020-12-23 13:25

    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).
    0 讨论(0)
  • 2020-12-23 13:27

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

    Press Ctrl + K and then X.

    0 讨论(0)
  • 2020-12-23 13:28

    Type ctor, and then press TAB twice.

    0 讨论(0)
提交回复
热议问题