Leo Vo
Leo Vo

Reputation: 10330

New feature in .NET Framework?

I have a research about feature "Data Access Application Block" in EntLib. I find some codes:

public static class MapBuilder<TResult> where TResult : new()
{
    //...
}

I don't understand about meaning of declaring of this class when using "where TResult : new()". I think this is new feature in .NET Framework to declare a class. Please give me a document or link to explain about this feature. Thanks.

Upvotes: 0

Views: 147

Answers (2)

Bertrand Marron
Bertrand Marron

Reputation: 22210

where TResult : new() adds a constraint on the generic type parameter (TResult). The type argument must have a parameterless constructor.

Upvotes: 1

George Duckett
George Duckett

Reputation: 32428

It's a generic type parameter constraint. It's been around since the introduction of generics, in .net 2.0.

Constraints on Type Parameters (MSDN)

The particular constraint you've mentioned means only a class with a public parameterless constructor can be used as the type TResult.


See also: Generics (MSDN)

Upvotes: 6

Related Questions