user3927775
user3927775

Reputation: 27

"where T : X, new()" or "where T : new(), X"?

What is the difference between the two following generic methods where in first method new() is written first and Book is written last and vice-versa for the last method.

public static void Save<T>(T target) where T : new(), Book
{
   ....
}

and

public static void Save<T>(T target) where T : Book, new()
{
   ....
}

where Book is a custom class.

Upvotes: 1

Views: 201

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

The difference is first one does not compile.

From MSDN:

where T : new() The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.

Upvotes: 15

Related Questions