Practice mode C#

Which generic constraint requires a type argument in C# to have a public parameterless constructor?

Correct answer C. where T : new()

Explanation

The new() constraint lets generic code create instances with new T(). It must appear last when combined with other constraints, as in where T : Shape, new(). where T : class requires a reference type and where T : struct a non-nullable value type, but neither guarantees a parameterless constructor you can call. There is no where T : default constraint for this purpose.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.