new() in method

c#, generics

Solution

It means that the type `T` must expose a public, default (i.e. parameterless) constructor. That is, you will be able to construct an instance of `T` with `new T()`. It can expose other constructors as well, but this generic constraint makes the default one mandatory.

Problem

Having these classes: ``` public interface IDbContextFactory { DbContext GetContext(); } public class Repo<T> : IRepo<T> where T : Entity, new() { protected readonly DbContext c; } public Repo(IDbContextFactory f) { c = f.GetContext(); } ``` What does the keyword `new()` (in `class Repo<T>`) do?

Original source