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 Tools → Options → Text Editor → C# → IntelliSense, under the Snippets behaviour section: Make sure "Always include snippets" is selected.
C# - Class , constructors and Resharper - clarification
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.A class can have any number of constructors... although it will become unwieldy pretty quickly.
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.
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:
Right click on the constructor you want to change, select Refactor->Change Signature.
Make the changes you require and click Next. Select "Resolve with call tree" and click Next.
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.
Double click on each of the classes and choose the "Create parameter x in constructor y"
Related Topics
Insert into C# with SQLcommand
.Net and Bitmap Not Automatically Disposed by Gc When There Is No Memory Left
Reading a Key from the Web.Config Using Configurationmanager
How to Pass Multiple Enum Values in C#
Getting All Changes Made to an Object in the Entity Framework
ASP.NET Core Model Binding Error Messages Localization
Is There Any Connection String Parser in C#
Namespace and Class with the Same Name
Filter All Queries (Trying to Achieve Soft Delete)
Except Has Similar Effect to Distinct
Static Binding Doesn't Update When Resource Changes
How to Store/Retrieve Rsa Public/Private Key
How to Find All the Classes Which Implement a Given Interface
How to Print Out a Tree Structure