C#
constructors
programming
Type
efficiency

Most efficient way to get default constructor of a Type

Master System Design with Codemia

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

Introduction

If you only need to discover whether a type has a public parameterless constructor, the direct reflection call is GetConstructor(Type.EmptyTypes). If you need to create instances many times, the real efficiency question is not just "how do I get the constructor?" but "how often am I doing reflection, and should I cache a factory delegate instead?" Those are related but different problems.

The direct reflection answer

For a public default constructor, the most direct lookup is:

csharp
1using System;
2using System.Reflection;
3
4public static class Demo
5{
6    public static void Main()
7    {
8        Type type = typeof(UriBuilder);
9        ConstructorInfo? ctor = type.GetConstructor(Type.EmptyTypes);
10
11        Console.WriteLine(ctor is not null);
12    }
13}

Type.EmptyTypes tells reflection you want the constructor with zero parameters. This is clearer and faster than building a new empty array every time.

Include non-public constructors when needed

If you also want private or protected parameterless constructors, use the overload that accepts binding flags.

csharp
1using System;
2using System.Reflection;
3
4public class HiddenCtor
5{
6    private HiddenCtor() { }
7}
8
9Type type = typeof(HiddenCtor);
10ConstructorInfo? ctor = type.GetConstructor(
11    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
12    binder: null,
13    types: Type.EmptyTypes,
14    modifiers: null
15);
16
17Console.WriteLine(ctor is not null);

That changes the meaning of the question from "public default constructor" to "any parameterless instance constructor," so be deliberate about which one you actually need.

Reflection lookup once, then cache

If you are doing this in a serializer, mapper, or dependency framework, repeated reflection lookup is usually the real cost. The standard optimization is to resolve the constructor once and cache either the ConstructorInfo or a compiled factory delegate.

csharp
1using System;
2using System.Collections.Concurrent;
3using System.Linq.Expressions;
4
5public static class ActivatorCache
6{
7    private static readonly ConcurrentDictionary<Type, Func<object>> Cache = new();
8
9    public static object Create(Type type)
10    {
11        var factory = Cache.GetOrAdd(type, BuildFactory);
12        return factory();
13    }
14
15    private static Func<object> BuildFactory(Type type)
16    {
17        var ctor = type.GetConstructor(Type.EmptyTypes)
18            ?? throw new InvalidOperationException("No public default constructor.");
19
20        var newExpr = Expression.New(ctor);
21        var body = Expression.Convert(newExpr, typeof(object));
22        return Expression.Lambda<Func<object>>(body).Compile();
23    }
24}

This is often much faster across many repeated activations than calling reflection lookup and invocation on every object creation.

If you know the type at compile time, prefer new()

When the generic type is known at compile time and the design allows it, a generic constraint is simpler than reflection.

csharp
1public static T Create<T>() where T : new()
2{
3    return new T();
4}

This avoids the runtime constructor lookup altogether and is usually the cleanest option when it fits the API.

Efficiency depends on what you mean by "get"

There are really three separate tasks:

  • discover whether the constructor exists
  • retrieve metadata for the constructor
  • instantiate objects quickly over time

GetConstructor(Type.EmptyTypes) is the right answer to the second task. A cached compiled delegate is the better answer to the third.

That is why many "most efficient" discussions talk past each other. They are answering different versions of the question.

Common Pitfalls

The biggest mistake is using reflection lookup inside a hot loop without caching. The one-time lookup is not the problem; the repetition is.

Another issue is forgetting whether you want only public constructors or also non-public ones. The wrong overload can silently change behavior.

Developers also sometimes use Activator.CreateInstance for everything without measuring. It is convenient, but convenience is not the same as best throughput in repeated-creation scenarios.

Finally, if the type is known generically at compile time, reflection is usually unnecessary complexity. A new() constraint is often better.

Summary

  • Use GetConstructor(Type.EmptyTypes) to retrieve a public parameterless constructor directly.
  • Use binding flags when you intentionally want non-public constructors too.
  • Cache constructor metadata or compiled factory delegates if you instantiate repeatedly.
  • Prefer a generic new() constraint when the type is known at compile time.
  • The fastest solution depends on whether you are discovering metadata once or creating objects many times.

Course illustration
Course illustration

All Rights Reserved.