Dissecting the new() constraint in C#: a perfect example of a leaky abstraction

Most likely you’ve heard about The Law of Leaky Abstractions coined by Joel Spolsky. Even if you never heard of it, you definitely faced it in your day-to-day job. The “law” is pretty simple: “All non-trivial abstractions, to some degree, are leaky”. And this is 100% true. But sometimes even not that complicated abstractions can leak their internal details.

Let’s consider the following code snippet:

public class NodeFactory
public static TNode CreateNode<TNode>()
where TNode : Node, new()
return new TNode();

Do you see any issues with it? Will it pass a thorough code review? Of course, you need to know the context. For instance, you need to know what the TNode types are, whether the constructor of those types can throw exceptions and whether the method can be called on a hot path of an app.

But first of all, you need to know what the compiler and the runtime will do with a method like this.

Once a user calls a method CreateNode, the C# compiler checks that a given type has a default constructor and if this is the case the compiler will emit a call to it. Right? Not exactly. The compiler doesn’t know upfront what constructor to call, so it delegates all the job to a helper method – Activator.CreateInstance<T> (*).

(*) This statement is not 100% correct. Different C# compilers emit different code for new T(). The C# compiler starting from VS2015 emits a call to the Activator.CreateInstance(), but older versions are “smarter”: they return default(T) for value types and calls the Activator.CreateInstance() only for reference types.

Ok, and what’s wrong with the Activator? Nothing, if you know how it’s implemented.

Implementation details of the Activator.CreateInstance

Non-generic version of the Activator.CreateInstance(Type) was first introduced in the .NET Framework 1.0 and was based on reflection. The method checks for a default constructor of a given type and calls it to construct an instance. We can even implement a very naïve version of this method ourselves:

public static T CreateInstance<T>() where T : new()
return (T) CreateInstance(typeof(T));

public static object CreateInstance(Type type)
var constructor = type.GetConstructor(new Type[0]);
if (constructor == null && !type.IsValueType)
throw new NotSupportedException($"Type '{type.FullName}' doesn't have a parameterless constructor");

var emptyInstance = FormatterServices.GetUninitializedObject(type);
return constructor?.Invoke(emptyInstance, new object[0]) ?? emptyInstance;

As we’ll see shortly an actual implementation of Activator.CreateInstance is a bit more complicated and relies on some internal CLR methods for creating an uninitialized instance. But the idea is the same: get a ConstructorInfo, create uninitialized instance and then call the constructor to initialized it, similar to the placement new concept in C++.

But the generic version “knows” the type being created at compile time so the implementation could be way more efficient, right? Nope. Generic version is just a façade that gets the type from its generic argument and calls the old method – reflection-based Activator.CreateInstance(Type).

You may wander: “Ok, for new T() the C# compiler calls Activator.CreateInstance<T>() that calls Activator.CreateInstance(Type) that uses reflection to do its job. Is it a big deal?” Yes, it is!

Concern #1. Performance

Using reflection to create a frequently instantiated type can substantially affect the performance of your application. Currently I work on build system and one of the components is responsible for parsing build specification files. The first implementation of the parser was used a factory method that created every node using new TNode() as shown above. The very first profiling session showed a sizable impact of the factory on the end-to-end performance. Just by switching to a more expression-based implementation of the node factory we gained 10% performance improvements for one of our end-to-end scenarios.

To be more specific, let’s compare different ways of creating a Node instance: explicit construction, using Func<Node>, Activator.CreateInstance and a custom factory based on the new() constraint.

public static T Create<T>() where T : new() => new T();
public static Func<Node> NodeFactory => () => new Node();

// Benchmark 1: ActivatorCreateInstace
var node1 = System.Activator.CreateInstance<Node>();
// Benchmark 2: FactoryWithNewConstraint
var node2 = Create<Node>();
// Benchmark 3: ConstructorCall
var node3 = new Node();
// Benchmark 4: FuncBasedFactory
var node4 = NodeFactory();

Here are the perf numbers obtained using BenchmarkDotNet:

                         Method |        Mean |    StdDev |  Gen 0 |
------------------------------- |------------ |---------- |------- |
        ActivatorCreateInstance |  98.6628 ns | 3.0845 ns |      - |
FactoryMethodWithNewConstraint | 103.0030 ns | 4.2670 ns |      - |
                ConstructorCall |   2.4361 ns | 0.0430 ns | 0.0036 |
               FuncBasedFactory |   6.8369 ns | 0.0436 ns | 0.0034 |

As we can see, the difference is pretty drastic: a factory method based on the new() constraint is 15 times slower than a delegate-based solution and 50 times slower than manual construction. But performance is not the only concern.


Reflection-based method invocation means that any exception thrown from the method will be wrapped in a TargetInvocationException:

class Node
public Node()
throw new InvalidOperationException();

public static T Create<T>() where T : new() => new T();

var node = Create<Node>();
Console.WriteLine("Node was create successfully");

catch (InvalidOperationException)
// Handling the error!
    Console.WriteLine("Failed to create a node!");

Is it obvious for everyone that the code shown above is incorrect? Reflection-based object construction “leaks” through the generics implementation. And now every developer needs to know how new T()is implemented and the consequences it has in terms of exception handling: every exception thrown from the constructor will be wrapped in a TargetInvocationException!

You may fix the issue if you know that the type’s constructor may throw an exception. Starting from .NET 4.5 you can use ExceptionDispatchInfo class to rethrow an arbitrary exception object (an inner exception in this case) without altering the exception’s stack trace:

public static T Create<T>() where T : new()
return new T();
catch (TargetInvocationException e)
var edi = ExceptionDispatchInfo.Capture(e.InnerException);
// Required to avoid compiler error regarding unreachable code

This code solves one issue with Activator.CreateInstance, but as we’ll see in a moment, there are better solutions that fix correctness as well as performance issues.

Correctness (2)

Activator.CreateInstance is implemented in a more complicated way than I mentioned before. Actually, it has a cache that holds constructor information for the last 16 instantiated types . This means that the user won’t pay the cost of getting the constructor info via reflection all the time, although it will pay the cost of a slow reflection-based constructor invocation.

A more accurate description of the algorithm used by Activator.CreateInstance is as following:

  1. Create a raw instance using RuntimeTypeHandle.Allocate(this)
  2. Get the ConstructorInfo for the given type’s parameterless constructor
    1. If the constructor information is already in the cache, get it from there
    2. If the constructor information is not in the cache, get a ConstructorInfo via reflection and put it into the cache
  3. Call the constructor on the newly created instance and return a fully constructed instance to the caller

But unfortunately, this optimization has an issue (reproducible in .NET 4.0 – 4.6.2): the optimization doesn’t handle structs with a parameterless constructor properly. Current C# compiler doesn’t support custom default constructors for structs. But the CLR and some other languages do: you may create a struct with a default constructor using C++/CLI or IL directly. Moreover, this feature was added to C# 6, but was removed from the language 3 months before the official release. And the reason is this bug in Activator.CreateInstance. Today there is a hot discussion at github about this feature, and it seems that even the language authors can’t agree on whether default constructors on structs is a good thing or not.

The issue is related to a caching logic in Activator.CreateInstance: if it gets the constructor information from the cache it doesn’t call the constructor for structs assuming, apparently, that they don’t exist (see InitializeCacheEntry method). And this means that if you have a struct with a default constructor, and you create an instance of that type multiple times, the constructor will only be called for the first instance.

We can’t easily fix the issues in Activator.CreateInstance and we definitely can’t change the existing behavior of new T() without breaking the world. But we can avoid using it and create our own generic factory that won’t suffer from the aforementioned issues.

Solution #1: using expression trees

Expression trees are a good tool for lightweight code generation. In our case, we can use an expression tree that creates a new instance of type T. And then we can compile it to a delegate to avoid performance penalty.

Lambda-expressions are special in the C# language because they’re convertible by the compiler to a delegate (DelegateType) or to an expression (Expression<DelegateType>). The compiler can convert an arbitrary expression to a delegate but only a limited set of language constructs can be converted to an expression. In our case the expression is very simple, so the compiler can cope with it:

public static class FastActivator
public static T CreateInstance<T>() where T : new()
return FastActivatorImpl<T>.NewFunction();

private static class FastActivatorImpl<T> where T : new()
// Compiler translates 'new T()' into Expression.New()
        private static readonly Expression<Func<T>> NewExpression = () => new T();

// Compiling expression into the delegate
        public static readonly Func<T> NewFunction = NewExpression.Compile();

FastActivator.CreateInstance is conceptually similar to Activator.CreateInstance but it lacks two main issues: it doesn’t suffer from the exception-wrapping problem and it doesn’t rely on reflection during the execution (it does rely on the reflection during expression construction, but this happens only once).

Let’s compare different solutions and see what we get:

                      Method |       Mean |    StdDev |  Gen 0 |
---------------------------- |----------- |---------- |------- |
     ActivatorCreateInstance | 94.6173 ns | 0.5036 ns |      - |
            FuncBasedFactory |  6.5049 ns | 0.0551 ns | 0.0034 |
FastActivatorCreateInstance | 22.2258 ns | 0.2240 ns | 0.0020 |

FastActivator is almost 5 times faster than the default one, but still 3.5 times slower than the func-based factory. I’ve intentionally removed the other cases we saw at the beginning; func-based solution is our base line, because any custom solution can’t beat an explicit constructor call for a known type.

The question is, why is the compiled delegate way slower than a manually-written delegate? Expression.Compile creates a DynamicMethod and associates it with an anonymous assembly to run it in a sandboxed environment. This makes it safe for a dynamic method to be emitted and executed by partially trusted code but adds some run-time overhead.

The overhead can be removed by using a constructor of DynamicMethod which associates it with a specific module. Unfortunately, Expression.Compile doesn’t allow us to customize the creation of a dynamic method and the only other option is to use Expression.CompileToMethod. CompileToMethod compiles the expression into a given MethodBuilder instance. But this won’t work for our scenario because we can’t create a method via MethodBuilder that has access to internal/private members of different assemblies. And this will restrict our factory to public types only.

Instead of relying on Expression.Compile we can “compile” our simple factory manually:

public static class DynamicModuleLambdaCompiler
public static Func<T> GenerateFactory<T>() where T:new()
Expression<Func<T>> expr = () => new T();
NewExpression newExpr = (NewExpression)expr.Body;

var method = new DynamicMethod(
            returnType: newExpr
new Type[0],

ILGenerator ilGen = method.GetILGenerator();
// Constructor for value types could be null
        if (newExpr.Constructor != null)
.Emit(OpCodes.Newobj, newExpr.Constructor);
LocalBuilder temp = ilGen.DeclareLocal(newExpr.Type);
.Emit(OpCodes.Ldloca, temp);
.Emit(OpCodes.Initobj, newExpr.Type);
.Emit(OpCodes.Ldloc, temp);

return (Func<T>)method.CreateDelegate(typeof(Func<T>));

The GenerateFactory method creates a DynamicMethod instance and associates that method with a given module. This immediately gives the method access to all internal members of the current assembly. But we specify skipVisibility as well, because the factory method should be able to create internal/private types declared in other assemblies as well. The name ‘lambda’ is never used and would be visible only during debugging.

This method creates an expression tree to get the constructor information even though we can get it manually. Note that the method checks newExpr.ConstructorInfo and uses different logic if the constructor is missing (i.e. for value types without a default constructor defined).

With the new helper method, FastActivator will be implemented in the following way:

public static class FastActivator
public static T CreateInstance<T>() where T : new()
return FastActivatorImpl<T>.Create();

private static class FastActivatorImpl<T> where T : new()
public static readonly Func<T> Create =

Let’s compare the new implementation (FastActivatorCreateInstance) with the expression-based one (CompiledExpression):

                      Method |       Mean |    StdDev |  Gen 0 |
---------------------------- |----------- |---------- |------- |
     ActivatorCreateInstance | 93.8858 ns | 1.2702 ns |      - |
            FuncBasedFactory |  6.4719 ns | 0.0640 ns | 0.0033 |
FastActivatorCreateInstance | 11.6035 ns | 0.0774 ns | 0.0030 |
          CompiledExpression | 22.7874 ns | 0.1509 ns | 0.0021 |

As we can see, the new version of the fast activator is two times faster than the old one, but still two times slower than the func-based factory. Let’s explore, why.

The reason is in implementation of the generics in the CLR. A generic method that calls a method from a generic type will never be inlined, so we suffer the overhead of an additional method call. But the more important thing is subtler. If the generic is instantiated with a value type the CLR has no other options except to generate a separate type for it. This means that a List<int> and a List<double> are completely independent from the CLR perspective. However, this is not the case with reference types. Two generic instantiations like List<string> and List<object> share the same EEClass, which allows the CLR to reuse the code between different instantiations and avoid code bloating. But this optimization trades speed for memory.

When you have one generic type (or method) that calls another generic type (or method) the CLR needs to make sure that actual types are compatible at runtime (**). To make sure that this is the case the CLR will make a few look-ups that affect the performance in the previous example and make our FastActivator slower than the delegate like () => new Node().

(**) The CLR implementation of generics is a very complicated topic and it’s definitely out of scope of this blogpost. If you want to understand the design of generics and the complexity of the problem better, I recommend to read an amazing article written by the author of the generics in .NET - Don Syme - Design and Implementation of Generics for the .NET Common Language Runtime. If you want to understand the current state of affairs, please see Pro .NET Performance or a very good article by Alexandr Nikitin, .NET Generics under the hood.

To prove this assumption let’s use the same factory on a value type Node:

                      Method |       Mean |    StdDev |  Gen 0 | Allocated |
---------------------------- |----------- |---------- |------- |---------- |
     ActivatorCreateInstance | 86.4298 ns | 2.5527 ns | 0.0005 |      12 B |
            FuncBasedFactory |  4.7406 ns | 0.0254 ns |      - |       0 B |
FastActivatorCreateInstance |  4.3134 ns | 0.0159 ns |      - |       0 B |
          CompiledExpression |  3.1534 ns | 0.0210 ns |      - |       0 B |

As we can see, there is no performance impact of the current solution when structs are involved.

To solve the issue with reference types we can avoid additional level of indirection and move the nested FastActivatorImpl<T> out from the façade FastActivator type and use it directly:

public static class FastActivator<T> where T : new()
/// <summary>
    /// Extremely fast generic factory method that returns an instance
    /// of the type <typeparam name="T"/>.
    /// </summary>
    public static readonly Func<T> Create =

And here are the last results when the FastActivator<T> is introduced and used directly:

                  Method |       Mean |    StdDev |  Gen 0 |
------------------------ |----------- |---------- |------- |
ActivatorCreateInstance | 95.0161 ns | 1.0861 ns | 0.0005 |
        FuncBasedFactory |  6.5741 ns | 0.0608 ns | 0.0034 |
  FastActivator_T_Create |  5.1715 ns | 0.0466 ns | 0.0034 |

As you can see we’ve achieved the goal and created a generic factory method with the same performance characteristic as a plain delegate that instantiates a specific type!

Application-specific fix for the Activator.CreateInstance issue

The C# compiler uses “duck typing” for many language constructs. For example, LINQ syntax is pattern based: if the compiler is able to find Select, Where and other methods for a given variable (via extension methods or as instance methods) it will be able to compile queries using a query comprehension syntax.

The same is true for some other language features, like the collection initialization syntax, async/await, foreach loop and others. But not everyone knows that there is a large list of “well known members” that the user may potentially provide to change the runtime behavior. And one of such well-known members is Activator.CreateInstance<T>.

This means that if the C# compiler is able to find another System.Activator type with a generic CreateInstance method then the given method will be used instead of the method from mscorlib. The following behavior is undocumented and I would not recommend using it in a production environment without clear evidence from a profiler. And even if a profiler shows some benefit , I would prefer using FastActivator explicitly instead on relying on this hack.

namespace System
/// <summary>
    /// Dirty hack that allows using a fast implementation
    /// of the activator.
    /// </summary>
    public static class Activator
public static T CreateInstance<T>() where T : new()

        Console.WriteLine("Fast Activator was called");

            return ActivatorImpl<T>.Create();

private static class ActivatorImpl<T> where T : new()
public static readonly Func<T> Create =

Now, all methods that call new T() to create an instance of a type, will use our custom implementation instead of relying on the default one.


This is a fairly long post, but we managed to cover many interesting details.

  • The new() constraint in the C# language is extremely leaky: in order to use it correctly and efficiently the developer should understand the implementation details of the compiler and the BCL.
  • We’ve figured out that the C# compiler calls Activator.CreateInstance<T> for creating an instance of a generic argument with a new() constraint (but remember, this is true only for C# 6+ compilers and the older versions emit the call only for reference types).
  • We’ve discovered the implications of the Activator.CreateInstance from a developer’s point of view in terms of correctness and performance.
  • We’ve come up with a few alternatives, starting with a very simple one that “unwraps” TargetInvocationException, to a fairly sophisticated solution based on code generation.
  • We’ve discussed a few interesting aspects of the generics implementation in the CLR and their impact on the performance (very minor, and likely negligible in the vast majority of cases).
  • And finally, we’ve come up with a solution that can solve aforementioned issues with the new() constrained by using the custom System.Activator.CreateInstance<T> implementation.

And as a final conclusion I won’t suggest that anyone removes all calls to new T() in their codebase or define their own System.Activator class. You need to profile your application and make the decision only based on real evidence.

But to avoid shooting yourself in the foot, you need to know what the compiler and the runtime do for new T() and other widely used language constructs and what the implications are from correctness and performance perspectives.

Comments (38)
  1. Michael Burbea says:

    You can actually further improve the performance of a dynamic method by using some security attributes.

    This will cause the dynamic method and expression tree to have the exact same performance profile.

    1. Thanks, Michael.

      I saw this solution when I’ve been working on my post. But I think that the proposed solution has few drawbacks:

      1. It doesn’t work for everyone. For instance, BenchmarkDotNet uses restricted environment (at least by default), so the solution didn’t work for me.
      2. Solution is application specific. It means that every client of the activator needs to add that attribute. This is ok, if we’re talking about application code, but this is definitely not ok for reusable piece of code.

      And last, but not least: I think this sentence is not correct – “further improve the performance”. I think using those attributes will remove the overhead for compiled expressions but using that attribute won’t get “better” performance comparing to hand-crafted dynamic method that I’m using in my solution. Right? Or I missing something?

  2. Daniel Stolt says:

    Amazing post, Sergey! Thanks for taking the time to write it up!

    1. Thanks, Daniel, my pleasure!

        1. Scritch says:

          amusing anecdote

          1. Stephen Kennedy says:

            Alternative Fact

  3. Jason Bock says:

    So have you submitted a PR to Roslyn such that it will use FastActivator instead of Activator.CreateInstance() in the future? 🙂

    1. Good idea:), but I’m sure that the PR will be rejected because they won’t break existing behavior…:(

      1. Matt Warren says:

        More likely, it would be rejected because it requires Reflection.Emit to be used.

      2. A pretty nice solution IMO would be to make Activator.CreateInstance() a JIT intrinsic. The JIT would probably have to emit a separate stub for each T though, and I don’t know if that’s a big deal. There’s the security stuff to handle too, but it could be done at JIT time.

  4. Awesome writeup! Thanks, Sergey.

  5. Evgeny says:

    Very good article. There are many answers but with many questions too ).
    Would you explain more clearly this code:
    // Constructor for value types could be null
    if (newExpr.Constructor != null)
    ilGen.Emit(OpCodes.Newobj, newExpr.Constructor);
    LocalBuilder temp = ilGen.DeclareLocal(newExpr.Type);
    ilGen.Emit(OpCodes.Ldloca, temp);
    ilGen.Emit(OpCodes.Initobj, newExpr.Type);
    ilGen.Emit(OpCodes.Ldloc, temp);

    What do you get here? Am I right, that we used else for struct type only? When we get value type with parameterless constructor we will call ilGen.Emit(OpCodes.Newobj, newExpr.Constructor), won’t we? Is it an expected behavior?

    1. Evgeny, you’re right. If a value type defines a parameterless constructor, then newExpr.Constructor would be not null and that constructor would be called. But if a struct doesn’t have a default constructor (which is almost the default), then the else block would be called and the initobj instruction would be used.

  6. Huberto Kusters says:


    Two Questions:

    1. Is it not so that because you added T: new() that therefor the check if (newExpr.Constructor != null) is always true?
    2. What about passing parameters like Activator.CreateInstance(type, params object[] arg) can?


    1. Hi Huberto,

      1. No, newExpr.Constructor can be null for value types like Int32 and many others that doesn’t have a “real” default constructor.
      2. The same approach can be used for Activator.CreateInstance(type, args) as well. But in this case, typesafety will gone, because C# langauge doesn’t have a facility to express such a constraint.

  7. Cao says:

    Is it possible to improve the performance of the new() constraint while still preserving the leaky abstraction behavior, like throwing TargetInvocationException?

    1. Cao,

      This is definitely possible. Actually, there is two ways to do that:

      1. Exception wrapping logic could be baked inside the generated delegate (complicated approach).
      2. Exception wrapping logic could be added to a FastActivator class:

      static class FastActivator
      public static T CreateInstance() where T: new()
      return FastActivatorImpl.Create();
      catch(Exception e)
      throw new TargetInvocationException(e);

  8. David Pine says:

    I can say that I learned several key things from this post and I am grateful for that. This was a delightful read, thank you for sharing!

    1. Thanks, David

      I’m glad that the post was helpful.

  9. Can you elaborate on exactly *why* in .Net 4.6 new() on a generic struct uses Activator.CreateInstance? That seems like a significant regression to me with no perceivable benefit.

    1. This is indeed a regression. But that was an optimization from the first place.
      And here is the reason why the behavior was changed: the C# compiler team implemented custom default constructors on struct for C# 6. To support this feature the compiler *have* to remove this optimization. Then 3 month before release date, the feature was cut due to an aforementioned issue with Activator.CreateInstance. But the behavior for generics wasn’t rolled back because the team is still thinking to add custom default constructors for structs.

      Hopefully this does make sense.

  10. Jim says:

    I aam ssure thnis artiicle haas touched alll thee internet viewers,
    iits reazlly really nie post onn builkding upp neew weblog.

    1. Tthannksss aaa llloooottt!:))

  11. Very nice post! Thanks.

  12. What’s up, its fastidious article regarding media print, we all know media is a
    enormous source of information.

  13. Very enlightening. I was surprised to see such a performance hit in a very common pattern with generics. But it’s good to know that the manual IL generation can still fix that (as it so often does). Thanks for the article!

    1. Thanks a lot, Alexander.

  14. Hernando says:

    What’s the point of having the new() constraint if the compiler have to resort to reflection to resolve the default constructor?
    It completely baffles me that one of the solutions is emitting some IL and that the CLR doesn’t actually do exactly that (or the equivalent of it) at runtime. The “new()” should work just like a regular interface constraint.

    Anyway, AWESOME post, it confirmed my findings while doing some optimizations and testing them with the CLRProfiler, when to my surprise Activator.CreateInstance started showing up like the most costly.

    In my case I was able to avoid completely the “new T()” calls altogether by refactoring some of the code while keeping the code generic.

  15. Can I just say what a relief to find an individual who genuinely knows what they are talking about over the internet.

    You definitely understand how to bring an issue to light
    and make it important. More and more people must read this and understand this side of
    your story. I was surprised you’re not more popular because you definitely possess the gift.

  16. Wonderful, what a webpage it is! This blog gives helpful facts to us, keep it up.

  17. Mehdy Haghy says:

    Activator.CreateInstance accepts type variables instead of a specific type.
    does FastActivator.CreateInstance accept type variables too ?

    1. It is possible to change code gen in the FastActivator to take a type as a variable. In this case the code would be slightly more complicated, but still not drastically different.

  18. Eu sou extremamente impressionado com suas habilidades de
    escrita como com o layout de seu blog . Este é um tema pago ou você personalizar é você mesmo?
    Enfim acompanhar o bom escrevendo com qualidade, é raro ver
    um grande blog como este estes dias .

  19. Well I definitely liked studying it. This post offered by you is very constructive
    for proper planning.

Comments are closed.

Skip to main content