J_PT J_PT - 1 month ago 14
C# Question

Base Class Generic Arguments Clarification

I have an Abstract class that is implemented like this:

public abstract class BaseImplementation<T, U> : IHistory<T>
where T : class, IEntity, new()
where U : DbContext, new()


I understand that the generic argument
<U>
is an EF
DbContext
.
I understand that the generic argument
<T>
must be a class that implements the
IEntity
Interface.

What is the
"new()"?
Must be a new instance of a given class? What is the purpose of that?
Note that is declared in both
<T>
and
<U>


Thank you.

Answer

new() is a constraint specifying that the type argument must have a public parameterless constructor. For more information about the generic type constraint, see MSDN

Comments