Intellij Generate Constructor Hot Key



I’ve gotten accustomed to many of the Java IDEs (Eclipse, Netbeans, IntelliJ) providing you with a command to generate a default constructor for a class based on the fields in the class.

For example:

Having a constructor populate the all of the fields of an object is such a common task in most OOP langauges, I’m assuming that there is a some way for me to save time writing this boilerplate code in C#. I’m new to the C# world, so I’m wondering if I’m missing something fundamental about the language? Is there some option in Visual Studio that is obvious?

141 time-saving Hotkeys for IntelliJ IDEA. Extensive, exportable, wiki-style reference lists for Keyboard Shortcuts/Hotkeys. Is there a keyboard shortcut to allow me to place my cursor on the method name that doesn't exist yet and have it create a method stub for it quickly? I don't want to use menus, I want to use a re.

Answers:

Resharper offers a Generate Constructor tool where you can select any field/properties that you want initialized. I use the Alt+Ins hot-key to access this.

Answers:

In visual studio 2015 Update3 I have this feature.

just by Highlighting properties and then press ctrl + . and then Press Generate Constructor.

For example if you’ve highlighted 2 properties it will suggest you to create a constructor with 2 parameters and if you’ve selected 3 it will suggest one with 3 parameters and so on.

also works with VS2017.

Answers:

C# added a new feature in Visual Studio 2010 called generate from usage. The intent is to generate the standard code from a usage pattern. One of the features is generating a constructor based off an initialization pattern.

The feature is accessible via the smart tag that will appear when the pattern is detected.

For example. Lets say I have the following class

And I write the following in my application

A constructor taking an int does not exist so a smart tag will show up and one of the options will be “Generate constructor stub”. Selecting that will modify the code for MyType to be the following.

Answers:

You could write a macro to do this — you would use Visual Studio’s parser to retrieve information about the class’s members.

I wrote a similar macro. (I’ll share the code below). The macro I wrote is for copying forward all of the constructors in a base class when you inherit from it (useful for classes like Exception that have lots of overloads on the ctor).

Hot

Here’s my macro (again, it doesn’t solve your problem, but you can probably modify to do what you want)

Intellij New File Shortcut

Answers:

Here is a macro that I use for that purpose. It will generate a constructor from fields and properties that have a private setter.

Answers:

Maybe you could try out this: http://cometaddin.codeplex.com/

Answers:

Here’s JMarsh‘s VS macro modified to generate a constructor based on the fields and properties in the class.

Answers:

I realize this is an old question, but if anyone is still looking for it you can do this easily with Resharper 8+. The ctorf, ctorp, and ctorfp snippets generate constructors that populate all the fields, properties, or fields and properties of a class.

Answers:

For VS 2015 I found an extension that does just this. It seems to work well and has a reasonably high amount of downloads. So if you can’t or don’t want to use Resharper you can install this one instead.

UPDATE

You can also acquire it via nuget

Answers:

I’m using the following trick:

Shortcuts In Intellij

I select the declaration of the class with the data-members and press:

Ctrl+C, Shift+Ctrl+C, Ctrl+V.

  • The first command copies the declaration to the clipboard,
  • The second command is a shortcut that invokes the PROGRAM
  • The last command overwrites the selection by text from the clipboard.

The PROGRAM gets the declaration from the clipboard,
finds the name of the class, finds all members and their types,
generates constructor and copies it all back into the clipboard.

We are doing it with freshmen on my “Programming-I” practice (Charles University, Prague)
and most of students gets it done till the end of the hour.

If you want to see the source code, let me know.

Tags: class, struct