Shortcut for creating constructor with variables (C# Visual Studio 2010)

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


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.


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.


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.