Generic classes
Default constructor
Syntax
Object-oriented programming
Programming basics

What is the syntax for a default constructor for a generic class?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A generic class does not need any special constructor syntax just because it is generic. If you want a default constructor, you write the same parameterless constructor you would write in a non-generic class. The confusion usually comes from mixing up two different ideas: a generic class having its own default constructor, and a type parameter being constrained to have a default constructor.

A Generic Class Can Have a Normal Default Constructor

In C#, a generic class with a default constructor looks completely ordinary.

csharp
1public class Box<T>
2{
3    public T Value { get; set; }
4
5    public Box()
6    {
7        Value = default!;
8    }
9}

And you can instantiate it like this:

csharp
var a = new Box<int>();
var b = new Box<string>();

The constructor syntax is just public Box(). The fact that the class has a type parameter does not change that part.

Java is similar in spirit:

java
1public class Box<T> {
2    private T value;
3
4    public Box() {
5        this.value = null;
6    }
7}

Again, there is nothing special about the constructor syntax itself.

The Real Source of Confusion: new() Constraints

In C#, you sometimes want to create an instance of T inside the generic class. That is a different question. For that, the type parameter must be constrained with where T : new().

csharp
1public class Factory<T> where T : new()
2{
3    public T Create()
4    {
5        return new T();
6    }
7}

Here, new() does not define the class constructor. It constrains the type argument so the compiler knows T has an accessible parameterless constructor.

This is the important distinction:

  • 'public Box() is the class constructor'
  • 'where T : new() is a constraint on the type argument'

They solve different problems.

What Happens Without the Constraint

If you try to instantiate T directly without telling the compiler that T has a default constructor, the code fails.

csharp
1public class Factory<T>
2{
3    public T Create()
4    {
5        return new T();
6    }
7}

That does not compile because not every possible T has a public parameterless constructor.

So if the original question came from code like new T(), the answer is not about constructor syntax for the class. The answer is about adding the new() constraint.

Default Constructor Versus Implicit Constructor

If you do not write any constructor at all, many languages provide an implicit default constructor.

C# example:

csharp
1public class Box<T>
2{
3    public T Value { get; set; }
4}

This class still has an implicit parameterless constructor because you did not define any constructor manually.

But if you add another constructor, the compiler no longer generates that implicit default constructor automatically.

csharp
1public class Box<T>
2{
3    public T Value { get; set; }
4
5    public Box(T value)
6    {
7        Value = value;
8    }
9}

Now new Box<int>() would fail unless you explicitly add public Box() yourself.

A Practical Example in C#

This combines both ideas clearly:

csharp
1public class CacheEntry<T> where T : new()
2{
3    public T Value { get; set; }
4
5    public CacheEntry()
6    {
7        Value = new T();
8    }
9}

Here:

  • 'CacheEntry<T> is a generic class'
  • 'public CacheEntry() is its default constructor'
  • 'where T : new() allows the constructor body to instantiate T'

That is often the exact pattern people are trying to write.

Common Pitfalls

  • Thinking a generic class needs a special constructor syntax. It usually does not.
  • Confusing the class's own default constructor with the new() constraint on T.
  • Forgetting that once you define another constructor, the implicit parameterless constructor is no longer generated automatically.
  • Trying to instantiate T without constraining it appropriately in C#.
  • Assuming the same generic-constructor rules apply identically across languages without checking the language-specific type-system rules.

Summary

  • A generic class uses the same ordinary parameterless constructor syntax as a non-generic class.
  • In C#, that syntax is just public ClassName().
  • The separate where T : new() rule is a type-parameter constraint, not the class constructor itself.
  • If you need to instantiate T, add the new() constraint.
  • Most confusion comes from mixing those two concepts together.

Course illustration
Course illustration

All Rights Reserved.