12

In Visual Studio 2010 C# you can, in a class, type ctor and then press tab and Visual Studio will create a constructor for that class for me. It is very convenient.

But is there a way to make Visual Studio create a constructor with all my variables, properties and so on?

For example,

public class User
{
    public String UserName { get; private set; }
}

And for this I want ctor + tab to make me a

public User(string UserName)
{
    this.UserName = UserName;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markus
  • 3,297
  • 4
  • 31
  • 52
  • 4
    Why would you want to create a constructor which violates .NET naming conventions? – Jon Skeet Jan 17 '12 at 11:42
  • @Markus - have you considered using the newer style object constructors in the generating client(s)? – Gayot Fow Jan 17 '12 at 11:55
  • 2
    @Garry Vass No, I don't even know what it is. – Markus Jan 17 '12 at 12:20
  • 2
    @Markus -> it's a lot cleaner than writing overloaded constructors in a lot of cases, read this: How to: Initialize Objects by Using an Object Initializer (C# Programming Guide) http://msdn.microsoft.com/en-us/library/bb397680.aspx – Gayot Fow Jan 17 '12 at 12:44
  • As a former java developer. I always look for outline pane to see methods in a pane(not in a combo-box) and source-> section in the context menu. I think Microsoft delegate these kind of jobs to 3rd parties, such as RSharper. If I were not see your question I would ask it. – Davut Gürbüz Apr 23 '13 at 05:16
  • 3
    @GarryVass: You cannot use object initializers with properties that have private setters. If you want to enforce the user of a data class to always set certain properties, there is no other way than to provide a constructor with the necessary parameters. – Tobias May 22 '13 at 13:31
  • In Visual Studio 2012 you have to press Tab two times to get the constructor inserted. – Peter Mortensen Mar 19 '19 at 00:37
  • cont' - Described in [in an answer to *Code snippet or shortcut to create a constructor in Visual Studio*](https://stackoverflow.com/questions/3873702/code-snippet-or-shortcut-to-create-a-constructor-in-visual-studio/3873731#3873731). – Peter Mortensen Mar 19 '19 at 02:12

8 Answers8

15

You can sort of do this the other way around; if you start without the constructor or field, and try to use the non-existent constructor, you can press ctrl+. to ask it to generate one for you, usage-first:

enter image description here

This compiler then generates something not too dissimilar:

public class User
{
    private string username;

    public User(string username)
    {
        // TODO: Complete member initialization
        this.username = username;
    }
}

You can then fix this up manually if needed (perhaps using the inbuilt rename refactor, etc). But not quite what you wanted.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Well the reason that I want such a snippet is that I have a quite large number of properties, and it would speed up the work a great deal if I had one. – Markus Jan 17 '12 at 12:05
10

I think what you are referring to is Code Snippets. You can write your own Code Snippets (they are written in XML). See here for a starting point.

You should also be able to edit existing Code Snippets (such as the ctor one). Refer to MSDN for some direction on this.

Note: Further Googling on Code Snippets will bring up more tutorials and references.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
4

Thanks to Samuel Slade (telling me it's called code-snippets) I managed to find another Stack Overflow answer: Snippet code to create constructor in VS2010 Express

And it seems as the answer is NO, not without a plugin/extension.

Many refer to the ReSharper extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markus
  • 3,297
  • 4
  • 31
  • 52
2

The "ctor" code snippet only creates a blank constructor, but does not use the existing attributes of the class in this constructor.

However, the latest versions of ReSharper enables you to choose the fields to be included in a constructor (like Eclipse does since a long time ago).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thiago Burgos
  • 947
  • 2
  • 13
  • 17
2

If you are using ReSharper the shortcut is Alt + Insert.

Source

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IndieTech Solutions
  • 2,527
  • 1
  • 21
  • 36
  • 1
    Sweet, I am actually using Resharper so that works for me! (However I won't mark it as a solution since you have to have it, and I know a lot of people don't) – Markus Jun 03 '15 at 10:05
  • Sorry I used to use this shorcut before, but now I reinstall visual studio and this combination doesn't work, and before I have never installed Resharper. – Ignacio Chiazzo Sep 17 '15 at 23:10
  • Make sure Resharper Shortcuts is the one by default in your VS – IndieTech Solutions Sep 18 '15 at 02:24
1

As others have noted, it is not possible to create snippets that are that intelligent.

There is a free Visual Studio add-in called Comet which can do what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LTR
  • 1,226
  • 2
  • 17
  • 39
  • CodePlex has been shut down now. The link is still somewhat alive (*"download archive"*), but perhaps update it if Comet now has a new location, etc. – Peter Mortensen Mar 19 '19 at 00:32
1

I think you could do this with a snippet:

See Creating and Using IntelliSense Code Snippets (MSDN)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andreas Rohde
  • 613
  • 5
  • 15
0

Use ReSharper's ctorf.

This will allow you to create a constructor with generated arguments based on fields defined in the class.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dirtyw0lf
  • 1,899
  • 5
  • 35
  • 63