Constructors are a necessary element of class design that typically involves repetitive work. Language teams try to reduce verbosity by moving patterns into language features. Where this terseness doesn’t exist, tools such as Telerik JustCode can alleviate some of the work to make a smooth coding experience. Today, I will introduce a few features that make creating constructors easier.

Blank Slate

Let’s start with a simple Employee entity class.


public class Employee
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }
    public string Department { get; set; }

A constructor typically uses its parameters to initialize necessary fields. These types of constructors can be quickly generated with JustCode by selecting one or more fields or properties, then using Create Constructor from the Code tab of the Visual Aid.


This creates the exact constructor necessary to initialize the fields and properties that were selected.


public Employee(int id)
    this.Id = id;

Selecting multiple properties and fields has the same effect. Highlight the appropriate lines then use Create Constructor.


public Employee(int id, string firstName, string lastName)
    this.Id = id;
    this.FirstName = firstName;
    this.LastName = lastName;

Enhancing Constructors

While instantiating a class, you may discover previously unknown use cases or you may realize that other properties were necessary to create an object in a valid state.

Suppose the Employee class’ constructor only required an id argument, as in the first example. It’s understandable if an employee hasn’t been assigned to a department or if they’re in the process of moving to a different city. But what does it mean if the employee has no name?


int id = 1;
string firstName = "Chris";
string lastName = "Eargle";
Employee employee = new Employee(id, firstName, lastName);


A constructor matching that signature does not exist, but JustCode gives you the ability to create the constructor as you are writing code that would use it.




There are two options on the quick fix list. Create Constructor will create an empty constructor that matches the signature. Create Constructor and Initialize Fields does the same but will initialize the fields as well. This fix uses the variable names from the argument list to match fields or properties. If there is no match, or if values are passed, it will create new fields on the class.

If there is only one additional argument, new options appear.




Add Parameter to Constructor At Position # will add the parameter to an existing constructor. Add Parameter to Constructor at Position # and Initialize Field will do the same and initialize the field.



Sometimes a less complex constructor is desirable with one of the parameters set to a default value.


public Employee(int id, string city)
    this.Id = id;
    this.City = city;

You can easily convert this to an appropriate overload with JustCode. Select the last parameter, then in the Visual Aid select Create Overload.


This will create the appropriate overload chaining the original constructor.


public Employee(int id) : this(id, null)

In this example, the second argument to the original constructor can be set to a default value.

public Employee(int id) : this(id, "Sofia")


Telerik JustCode makes you more productive by reducing the amount of code you must type by hand. It features quick ways to create the constructors you need, as you are working with that piece of code.

If you don’t have JustCode, give it a try today!

About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Related Posts


Comments are disabled in preview mode.