How to Generate a Constructor from Class Fields Using Visual Studio (And/Or Resharper)

How do I generate a constructor from class fields using Visual Studio (and/or ReSharper)?

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.

Auto-generate constructor from fields

It seems to have changed a bit in functionality from VS 2017 and later. Set the cursor on the class body (or declaration, i.e. public class MyClass...) and execute the same key strokes and you'll get "Generate Constructor" from the Quick Actions menu. Sometimes though it doesn't work and you have to press search Quick Actions and find it manually.

Automatically Create Constructor Using Fields/Properties in Visual Studio (like Eclipse does)

No. There is the ctor snippet(not quite what you were looking for), or you can create your macro. Possibly check out Productivity macros for C#. And since you don't like ReSharper, you can use CodeRush.

How to auto-generate constructors with ReSharper for many classes?

Using the "Find and Replace" function with regular expressions I created a quick and dirty solution:

Find what: {class }{[a-zA-Z]+}{[\n ]*\{}

Replace with: \1\2\3\ninternal \2(int x){}

Typing ctor to create constructor

Can you please check your Code Snippets Manager (Ctrl + K, Ctrl + B) if there are Visual C# file in Csharp language?

Code Snippets Manager

Go to menu ToolsOptionsText EditorC#IntelliSense, under the Snippets behaviour section: Make sure "Always include snippets" is selected.

C# - Class , constructors and Resharper - clarification

  1. Any extra burden to the compiler is basically irrelevant - it should not be part of your decision about whether or not to use var. As noted in comments, it may well require slightly more work for the compiler when you use explicitly declared variable... but again, it's not going to be significant.

  2. A class can have any number of constructors... although it will become unwieldy pretty quickly.

  3. The static constructor will be called once, before the first use of the class (whether that's via a static method or a constructor call). Read the C# spec for more details - section 10.12 of the C# 5 spec includes:

    The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

    • An instance of the class type is created.
    • Any of the static members of the class type are referenced.
  4. You can configure ReSharper to suggest alternatives, or treat them as warnings, etc. Make it work however you think it should, on this front.

Resharper (or Visual Studio) shortcut to cascade changes to a constructor

Since I asked this question Resharper have now added support as part of the Change Signature feature.

For example, to add a parameter to a constructor in the base class, use the following method:

  1. Right click on the constructor you want to change, select Refactor->Change Signature.

  2. Make the changes you require and click Next. Select "Resolve with call tree" and click Next.

  3. Your change will be made, and a window will open somewhere in Visual Studio titled "Refactoring - Change signature". A list of inheriting classes will be shown in this window.

  4. Double click on each of the classes and choose the "Create parameter x in constructor y"



Related Topics



Leave a reply



Submit