Resharper, the terrific Visual Studio productivity plugin, reduces adding a constructor for class, with as many arguments as you need, to just a few mouse clicks.Here’s how to use this Resharper feature.
1public class Artist
2{
3 public int ArtistId { get; set; }
4 public string Name { get; set; }
5 public int Rank { get; set; }
6}
Put the cursor somewhere in the body of a class that needs a constructor and press Alt/Ins (or use the Resharper>Code>Generate menu option). This displays Resharper’s Generate context menu (as shown below). Click “Constructor.”
Coding with Resharper is almost like pair programming but minus the gum-chewing, Jonas Brother-listening, obnoxious coder next to you.
This shows the Generate constructors dialog below. Select the members you want as arguments to the constructor with the checkboxes (selecting the class name selects all of the fields for use as constructor parameters). You can also select the access rights (public, private, et al) as well as whether you want an XML comment block added. Resharper is context sensitive–when using it to inject a constructor in an abstract class it’s smart enough to mark that constructor protected. Why? Read this. For my money, providing this level of awareness is how Resharper elevates itself from being a simple code paster to being a true ally in helping you write better code. It’s almost like pair programming but minus the gum-chewing, Jonas Brother listening, obnoxious coder next to you.
The “Advanced” button lets you specify what arguments are passed on to a base constructor (if any are available).
Assuming that the ArtistId and Name fields were selected, when you click “Finish” the constructor shown below is immediately added to the class.
1public class Artist
2{
3 public Artist( string name, int artistId )
4 {
5 Name = name;
6 ArtistId = artistId;
7 }
8
9 public int ArtistId { get; set; }
10 public string Name { get; set; }
11 private int Rank { get; set; }
12}
As you can see, Resharper makes it ridiculously easy to automate constructor creation, with optional parameters and value assignments. Although my examples show Resharper working with C#, there is a VB.NET version of Resharper available.



